.Net6.0 使用FastReport
在.NET 6.0中使用FastReport需要安装FastReport的.NET版本,并在项目中引用FastReport的相关程序集。
添加NuGet包步骤:
(1) 打开解决方案项目;
(2) 在顶部菜单栏点击【功能(T)】
(3) 在弹出的菜单窗口点击【NuGet包管理器(N)】
(4) 在子菜单中点击【管理解决方案的NuGet程序包(N)…】
(5) 弹出NuGet-解决方案界面,如下图
(6) 点击【浏览】,然后在输入框中,输入FastReport,在联网的情况下,VS会自动搜索出相关的包;如图在列表中找到并选择FastReport.Net.Demo;勾选需要安装的项目,然后点击安装按钮,等待安装完成即可;
(7) 安装完成后,展开依赖项可以查看使用的库文件以及依赖库,如下图
1. 打印.frx文件
FastReport.Report report = new FastReport.Report();//初始化对象
report.PrintSettings.Printer = PrinterName;//打印机名称
report.Load(@"D:\Working_ASVN\测试模板.frx");//加载模版
//根据模板数据将数据绑定或注册到.frx中
// report.RegisterData(m_StudentInfo, "StudentInfo");// m_StudentInfo一个类的字段列表
FastReport.EnvironmentSettings eSet = new FastReport.EnvironmentSettings();
eSet.ReportSettings.ShowProgress = false;//取消进度对话框显示
report.PrintSettings.ShowDialog = false;//取消打印设置对话框显示
report.Print();//打印报告
2. 导出成pdf
FastReport.Report report = new FastReport.Report();
BindPrintReport(report, true);//自己实现的函数,将数据注册到FastReport对应的类数据中
report.Prepare();
PDFExport export = new PDFExport();
export.JpegCompression = true;
report.Export(export, dlg.FileName);
ClearReport(report);
report.Clear();
report.Dispose();
report = null;
扩展:把FastReport相关的依赖库文件全部找出放一个文件夹中,通过添加项目引用的方式把所有的程序集引用到本地项目中,其他的实现代码都是一样的。
注:在打印实现时,设置隐藏进度对话框显示和打印设置对话框显示后,在VS2022调试时,会出现故障(System.Runtime.InteropServices.SEHException:“External component has thrown an exception.”),但是直接运行exe程序时,又是正常的,这具体原因我也不知道。
**************************************************************************************************************