今天用到了一个报表控件(FastReport),刚接触有些可能自己理解的不太对,代码仅供参考(winfrom项目):
使用前要去下载一下FastReport的引用包
FastReport.dll,
FastReport.Editor.dll,
FastReport.Bars.dll
引用这三个dll就可以使用了;
//用来标识是直接打印还是进入模板设置
private bool IsDesign = false;
/// <summary>
/// 打印方法
/// </summary>
private void Print()
{
FastReport.Report rep = new FastReport.Report();
rep.Load(Application.StartupPath + @"\Reports\NewPatientInfo.frx"); //这个需要先设计一个格式,并生成frx文件,路径和文件名根据自己项目而定,不然直接打印会报错
DataSet dsprint = new DataSet();
dsprint.Clear();
dsprint.Reset();
//得到需要打印的数据,我是打印当前DataGridView控件中的数据
DataTable dtpat = new DataTable();
dtpat = (this.dgvInfo.DataSource) as DataTable;
dtpat.TableName = "TablePat";
//不能写成dsprint.Tables.Add(dtpat);,这样的话当第二次调用时会报“DataTable 已属于另一个 DataSet”
dsprint.Tables.Add(dtpat.Copy());
rep.RegisterData(dsprint);//把需要打印的DataSet给FastReport控件
rep.PrintSettings.ShowDialog = false;//关闭打印设置弹窗(如果需要可以改成true)
//判断是直接打印还是进入模板设置
if (!IsDesign)
{
rep.Print();
}
else
{
rep.Design();
}
}
/// <summary>
/// 打印按钮
/// </summary>
private void btnPrint_Click(object sender, EventArgs e)
{
//直接打印
this.IsDesign = false;
Print();
}
/// <summary>
/// 模板设置按钮
/// </summary>
private void btnPrintSet_Click(object sender, EventArgs e)
{
//模板设置
this.IsDesign = true;
Print();
}
这样就可以使用了,后续应该会改进。
这里有一个在FastReport上,操作数据源生成模板的操作说明,可参考一下:(http://www.cnblogs.com/dotnet261010/p/9136767.html)