栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# 使用FastReport.NET打印报表

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 使用FastReport.NET打印报表

文章目录
  • 一、设计报告单
    • 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命名空间

/// 
/// 打印报告列表实体类
/// 
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 List DataViewModel { 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);
}
1.2 报告单中选择数据源

在代码中注册数据源用编辑模式打开报告单模板后,选择“Data” >> “Choose Report Data…” >> “OK”,就完成了数据源的添加。如下:

2.设置数据区域的单元格自动高度

选中要设置自动高度的单元格,在属性中设置“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()
		};
		// 报告单数据源
		List printModels = 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);
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023949.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号