C#调用FastReport报表打印时的传参形式

 

C#调用FastReport报表打印时的传参形式

方式一

调用SetParameterValue方法

FastReport.Report report = new FastReport.Report();

report.SetParameterValue(参数名称, 参数值);

需要在报表模板文件xx.frx中的<Dictionary>标签中添加对应的参数

 

在报表设计器中调用参数的方法如下

 

方式二

调用RegisterData注册数据

report.RegisterData(数据集, 数据集名称);

需要在报表模板文件xx.frx的<Dictionary>标签中添加表格数据源、定义数据列及类型

 

在报表设计器中调用数据集的方法如下

 

调用实例

 

1.直接传参数

FastReport.Report report = new FastReport.Report();

report.Load("ReportFile\\1.直接传参数.frx");//加载模板

report.PrintSettings.Printer = Printer;//设置打印机

string Username = "直接传递参数";

report.SetParameterValue("prams1", Username);//传参

report.Show();//预览

效果:

 

2.DataSet传参

FastReport.Report report = new FastReport.Report();

            report.Load("ReportFile\\2.DateSet传参.frx");

            report.PrintSettings.Printer = Printer;

            //组织数据

            DataSet dt = new DataSet();

            DataTable tb = new DataTable();

            tb.TableName = "成绩表";

            tb.Columns.Add(new DataColumn("语文", Type.GetType("System.Int32")));

            tb.Columns.Add(new DataColumn("数学", Type.GetType("System.Int32")));

            tb.Columns.Add(new DataColumn("英语", Type.GetType("System.Int32")));

            for (int i = 0; i < 10; i++)

            {

                DataRow dr = tb.NewRow();

                dr["语文"] = 36;

                dr["数学"] = 89;

                dr["英语"] = 82;

                tb.Rows.Add(dr);

            }

            dt.Tables.Add(tb);

            //数据注册

            report.RegisterData(dt, "Data");

            report.Show();

效果:

 

3.图片传输

FastReport.Report report = new FastReport.Report();

            report.PrintSettings.Printer = Printer;

            report.Load("ReportFile\\3.图片传输.frx");

            //直接传图

            Image image = Image.FromFile("test.jpg");

            FastReport.PictureObject imgobj = report.FindObject("Picture1") as FastReport.PictureObject;

            imgobj.Width = 120;

            imgobj.Height = 120;

            imgobj.Image = image;

            //DateSet传图

            byte[] img = ImageToByte(image);

            DataSet dt = new DataSet();

            DataTable tb = new DataTable();

            tb.TableName = "图片表";

            tb.Columns.Add(new DataColumn("图片1", Type.GetType("System.Byte[]")));

            DataRow dr = tb.NewRow();

            dr["图片1"] = img;

            tb.Rows.Add(dr);

            dt.Tables.Add(tb);

            report.RegisterData(dt, "Data");

            report.Show();

 效果:

 

 

 

posted @ 2022-01-05 14:11  Mosity  阅读(1966)  评论(0编辑  收藏  举报