VS2015同2010下调用FastReport报表打印预览源码
private void Form1_Load(object sender, EventArgs e)
{
Report report = new Report();
report.Load("1.frx");
report.RegisterData(CommonCode.GetDataSet().Tables[0], "person");
report.Preview = previewControl1;
report.Prepare();
report.ShowPrepared();
}
解释:
实例化对象
为对象做如下匹配:
加载frx模板: 启用FastReport Designer, 建数据源 拖字段 加参数 美化报表等 以及 将报表保存到项目目录 并将模板文件右K的属性改为总是复制到启用目录(很重要,否则是无法加载这个模板文件的)
注册数据: CommonCode.CS为程序员手工编写在实例类中的表达方法,见下面代码
匹配预览控件
准备
显示准备
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace _02FastReport报表
{
public class CommonCode
{
static string connStr = "server=.;uid=sa;pwd=123;database=test;";
public static DataSet GetDataSet()
{
using (SqlConnection conn = new SqlConnection(connStr))
{
//conn.Open();//这个不需要在using下
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from person;", conn);
da.Fill(ds);
return ds;
}
}
public static DataTable GetDataTable()
{
using (SqlConnection conn = new SqlConnection(connStr))
{
//conn.Open();//这个不需要在using下
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select top 2 * from person;", conn);
da.Fill(ds);
return ds.Tables[0];
}
}
}
}
===========================================找点
private void PrintHZPackOne(HZTagModelInfo hZTagModelInfo, HZPackOneInfo one_insert)
{
try
{
Report report= new Report();//打印类
report.Clear();//清除缓存
report.Load(@"Print\HZPackOne.frx");//加载模板
var ProduceName = report.FindObject("ProduceName") as TextObject;//产品名称
if (ProduceName != null)
{
ProduceName.Text = one_insert.TagName;
}
var Barcode1_SN = report.FindObject("Barcode1_SN") as BarcodeObject;//一维码
if (Barcode1_SN != null)
{
Barcode1_SN.Text = one_insert.SN;
Barcode1_SN.Barcode = new Barcode128();
}
var Barcode2_SN = report.FindObject("Barcode2_SN") as BarcodeObject;//二维码
if (Barcode2_SN != null)
{
Barcode2_SN.Text = one_insert.SN;
Barcode2_SN.Barcode = new BarcodeQR();
}
report.PrintSettings.ShowDialog = false; //直接打印 True时先预览再打印
report.Print();
report.Dispose();
}
catch (Exception ex)
{
XtraMessageBox.Show("打印出错!\r\n" + ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}