- 一、设计报告单
- 1.给报告单添加数据源Data Source
- 1.1 代码中注册数据源
- 1.2 报告单中选择数据源
- 2.设置数据区域的单元格自动高度
- 二、打印报告单代码示例
FastReport.NET官方下载地址: https://www.fast-report.com/en/download/fast-report-net/
要打印的报告单格式如下:
安装完FastReport.NET后,就可以设计报告单,要设计的报告单格式如下:
1.给报告单添加数据源Data Source 1.1 代码中注册数据源首先应该在项目中引用FastReport.dll,然后使用代码给报告单注册数据,如下:
using FastReport; // 引用FastReport命名空间 ///1.2 报告单中选择数据源/// 打印报告列表实体类 /// private class DataViewModel { public string TestItem { get; set; } public string ItemID { get; set; } public string WriteFormatData { get; set; } public string ReadFormatData { get; set; } public string Evl { get; set; } } ////// 打印报告实体类 /// private class PrintModel { public string Number { get; set; } public string CarTypeLib { get; set; } public string TestUser { get; set; } public ListDataViewModel { get; set; } } // 报告单数据源 List printModels = new List (); // 数据源 printModels.Add(model); // 调用FastReport打印报告单 try { Report report = new Report(); report.Load(Application.StartupPath + "\ECU13诊断报告.frx"); // 加载报告单模板 report.RegisterData(printModels, "PrintModels"); // 注册Data Source数据源 //report.Show(); // 报告单预览模式 report.Design(); // 报告单编辑模式 report.Dispose(); } catch (Exception ex) { MessageBoxEx.Show($"打印失败!{ex.ToString()}", "错误", MessageBoxButtonsEx.OK, MessageBoxIconEx.Error); }
在代码中注册数据源用编辑模式打开报告单模板后,选择“Data” >> “Choose Report Data…” >> “OK”,就完成了数据源的添加。如下:
选中要设置自动高度的单元格,在属性中设置“CanGrow”和“GrowToBottom”为“True”,如下:
打印时需获取要打印的数据,组织打印数据的格式,向报告单中注册数据等,代码中有详细的注释。如下:
////// 打印按钮点击事件 /// /// /// private void btnPrint_Click(object sender, EventArgs e) { if (dgvTestRecord.CurrentRow == null) { MessageBoxEx.Show("请先选择一条检测记录进行打印!", "提示", MessageBoxButtonsEx.OK, MessageBoxIconEx.Information); return; } DialogResult dialogResult = MessageBoxEx.Show($"确定要打印序列号为{dgvTestRecord.CurrentRow.Cells["Number"].Value}的报告单吗?", "询问", MessageBoxButtonsEx.YesNo, MessageBoxIconEx.Question); if (dialogResult == DialogResult.Yes) { Conditions conditions = GetQueryConditions(); // 自定义的类,封装查询条件 conditions["Number"] = dgvTestRecord.CurrentRow.Cells["Number"].Value.ToString(); conditions["TestTimes"] = dgvTestRecord.CurrentRow.Cells["TestTimes"].Value.ToString(); var lst = ecu13TestRecord.GetList(conditions, orderby: "testendtime asc").ToList(); // 获取要打印的数据 // 组织报告单打印模板需要的数据 PrintModel model = new PrintModel { Number = dgvTestRecord.CurrentRow.Cells["Number"].Value.ToString(), CarTypeLib = dgvTestRecord.CurrentRow.Cells["CarTypeLib"].Value.ToString(), TestUser = dgvTestRecord.CurrentRow.Cells["TestUser"].Value.ToString(), DataViewModel = lst.Select(x => new DataViewModel { TestItem = x.testitem, ItemID = x.itemid, WriteFormatData = x.testitem == "CAN2" ? "————" : x.writeformatdata, ReadFormatData = x.readformatdata, Evl = x.evl }).ToList() }; // 报告单数据源 ListprintModels = new List (); printModels.Add(model); // 调用FastReport打印报告单 try { Report report = new Report(); report.Load(Application.StartupPath + "\ECU13诊断报告.frx"); report.RegisterData(printModels, "PrintModels"); report.Show(); // 报告单预览模式 //report.Design(); // 报告单设计模式 report.Dispose(); } catch (Exception ex) { MessageBoxEx.Show($"打印失败!{ex.ToString()}", "错误", MessageBoxButtonsEx.OK, MessageBoxIconEx.Error); } } }



