您的位置:首页 > 其它

WPF StimulSoft Reports 2014 初探。

2015-08-24 13:40 239 查看
初学StimulSoft Reports报表工具、其他的不谈,只谈谈如何打印出一张只有List或者Datatable的报表

首先从网上下载破解版的stimulsoft reports,进行安装

一、 安装完成后去安装目录下查找三个文件

我的安装目录:C:\Program Files (x86)\Stimulsoft Reports.Ultimate 2014.3\Bin

1、Stimulsoft.Base.dll

2、Stimulsoft.Report.dll

3、Stimulsoft.Report.Wpf.dll

二 、将这三个文件引用到WPF工程中

三、要打印的数据结构List等将最终转成DataTable,其他的没有做测试、目前测试用DataTable

//报表
private void ButtonReport_Click(object sender, RoutedEventArgs e)
{
List<StudentModel> list = new List<StudentModel>() 
            {
                new StudentModel(){Age = 19,Sex = "男",Class = "一班",Score = 92.658M,Remark = "此人年龄不足20!"},
                new StudentModel(){Age = 17,Sex = "女",Class = "二班",Score = 22.5258M,Remark = "此人不及格!"},
            };
            DataSet ds = new DataSet();
            ds.Tables.Add(list.ToDataTable());

            string FileName = Global.LocalPath + "\\Report2.mrt";

            StiReport report = new StiReport();
            report.RegData("CL", ds);
            report.Load(FileName);
            report.Compile();
            report["Title"] = string.Format("这是我的报表({0})", "第一年级");
            report.ShowWithWpf();
}
<span style="white-space:pre">	</span>/// <summary>
        /// List to DataTable扩展
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class
        {
            //创建属性的集合
            //获得反射的入口
            Type type = typeof(TResult);
            DataTable dt = new DataTable();
            //把所有的public属性加入到集合 并添加DataTable的列
            PropertyInfo[] propertis = type.GetProperties();
            foreach (PropertyInfo property in propertis)
            {
                Type colType = property.PropertyType;
                if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }
                dt.Columns.Add(property.Name, colType);
            }
            //Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });      
            foreach (var item in value)
            {
                //创建一个DataRow实例
                DataRow row = dt.NewRow();
                //给row 赋值
                foreach (PropertyInfo p in propertis)
                {
                    row[p.Name] = p.GetValue(item, null);
                }
                //pList.ForEach(p => row[p.Name] = p.GetValue(item));
                //加入到DataTable
                dt.Rows.Add(row);
            }
            return dt;
        }


四、关于如何设计报表

1、打开设计器

我的安装目录下C:\Program Files (x86)\Stimulsoft Reports.Ultimate 2014.3\Bin\Designer.Wpf.exe

打开后选择-文件-新建-标准报表



2、数据源-就是要打印的字段列

(1)新建数据源



(2)、选择列-注意:所谓别名就是让界面显示的名称。本文采用英文名称,汉字显示

点击 类似“abc”那个按钮添加列。添加过程中不要手贱的去点右下角的确定。。否则你还得重头来。



(3)选择所有的列点击下一步。(可以一步到底。因为接下来的对我们意义不大)

“列的顺序” - 没什么实际意义

排序 - 将会根据你选择的列来排序

Themes 这一列 我们选择None - 因为你还要编辑整个报表的外表。所以不建议选择主题。冲突有点厉害

完成后进入下一个阶段

3、报表设计 - 看到了是这个样子。

先不要着急做:

请看右下角有么有三个标签

1、属性 - 就像WPF属性管理器一样。可以设置控件的属性(字体、边框、绑定的数据源)、点击左边编辑器可以在右边属性标签看到属性

2、字典里有必要的系统变量、还有系统函数

3、报表树 - 这个可以快速查找控件



那么问题来了?

我们如何改一下标题!? 我需要是个变量的标题,不能写死

这时候就需要新建一个变量了!



新建变量之后你就可以直接操作报表的变量了 比如我们创建的变量是Title

代码是这样的

report["Title"] = “sadadads”;

具体代码顺序请返回到顶部查看



报表基本介绍完毕
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: