一、实现效果
RDLC文件开发报表的项目工程(源码)下载https://download.csdn.net/download/xiaochenXIHUA/73204877实现自定义的报表内容(自定义报表的显示格式内容),根据数据自动填充,效果如下:
二、实现思路
①创建报表【设置报表的大小和根据自己需求设计样式和内容】
②添加ReportViewer组件【编写加载报表方法】
三、实现方法
3.0、VS2019中安装ReportViewer组件
在VS2019中安装ReportViewer组件https://blog.csdn.net/xiaochenXIHUA/article/details/122268610?spm=1001.2014.3001.5501
3.1、创建报表
选中项目,点击鼠标右键-->选择【添加】-->选择【新建项】-->选择【报表】(可修改报表名称)
3.2、根据自身需求设计报表样式
3.2.1、添加数据集
①首先创建数据集(我这里是使用自定义DataTable数据,也可以使用数据库)
选择项目,点击鼠标右键-->选择【添加】-->选择【新建项】-->选择【数据集】(可修改数据集名称)
?②创建好数据集后,从工具箱中拖入DataTable到数据集界面中;然后选中DataTable1组件,点击鼠标右键-->选择【添加】-->选择【列】,然后输入对应的列名称即可,我这里设计的内容如下:
?3.2.2、设计自定义的报表
首先 添加前面设计好的数据集,然后修改报表的大小(根据纸张实际大小填写);接着设计报表(比如插入表、文本框等内容)
?最终设计效果如下:
?注意:如果中文显示不出来则是字体的原因,修改为(黑体、宋体等字体即可),如下操作:
?
?3.3、在窗体中添加RDLC报表
3.3.1、首先添加ReportViewer组件到窗体中?
?3.3.2、编写窗体中添加RDLC报表逻辑
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace Test_Winform_rdlc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadWarehouseOrder();
}
/// <summary>
/// 加载入库单
/// </summary>
private void LoadWarehouseOrder()
{
WarehouseOrderRdlc warehouseOrderRdlc = new WarehouseOrderRdlc("云南咖啡科技有限公司产品入库单",
"YNCoffee20211225", "物料管理部", "MN001", "2021-12-26 16:26:26",
"XMSCF001", "现磨小粒咖啡001", "1800", "盒", "杨思语", "宋莉莉", "李艳秋");
DataTable dt = GetRdlcReportData(warehouseOrderRdlc);
string rdlcFilePath = AppDomain.CurrentDomain.BaseDirectory + "/ReportLables/WarehouseOrder.rdlc";
LoadReportViewer(dt, rdlcFilePath);
}
/// <summary>
/// 获取rdlc报表数据
/// </summary>
/// <param name="wr">入库单实体</param>
/// <returns></returns>
private DataTable GetRdlcReportData(WarehouseOrderRdlc wr)
{
if (wr == null ) return null;
DataTable dt = GetRdlcReportDtModel();
int columnCount = dt.Columns.Count;
dt.Rows.Add(wr.TitleName, wr.OrderNumber, wr.Department, wr.Warehouse, wr.Date,
wr.MaterialNumber, wr.MaterialDesc, wr.TotalNumber, wr.Unit,
wr.ReportMaker, wr.MaterialStoragePeople, wr.CheckerName);
return dt;
}
/// <summary>
/// 获取rdlc报表模板
/// </summary>
/// <returns></returns>
private DataTable GetRdlcReportDtModel()
{
DataTable dt = new DataTable();
dt.Columns.Add("TitleName", typeof(string));
dt.Columns.Add("OrderNumber", typeof(string));
dt.Columns.Add("Department", typeof(string));
dt.Columns.Add("Warehouse", typeof(string));
dt.Columns.Add("Date", typeof(string));
dt.Columns.Add("MaterialNumber", typeof(string));
dt.Columns.Add("MaterialDesc", typeof(string));
dt.Columns.Add("TotalNumber", typeof(string));
dt.Columns.Add("Unit", typeof(string));
dt.Columns.Add("ReportMaker", typeof(string));
dt.Columns.Add("MaterialStoragePeople", typeof(string));
dt.Columns.Add("CheckerName", typeof(string));
return dt;
}
/// <summary>
/// 加载报表
/// </summary>
/// <param name="dt">需呈现的报表</param>
/// <param name="rdlcReportPath">rdlc报表文件路径</param>
/// <param name="dataSourceName">rdlc报表文件对应的数据源名称</param>
private void LoadReportViewer(DataTable dt,string rdlcReportPath, string dataSourceName = "DataSet1")
{
if (dt == null || string.IsNullOrEmpty(rdlcReportPath) || string.IsNullOrEmpty(dataSourceName)) return;
reportViewer1.LocalReport.ReportPath = rdlcReportPath;
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataSourceName, dt)); //绑定数据源
reportViewer1.RefreshReport();
}
//入库单
public class WarehouseOrderRdlc
{
/// <summary>
/// 标题名称
/// </summary>
public string TitleName { get; set; }
/// <summary>
/// 订单编号
/// </summary>
public string OrderNumber { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string Department { get; set; }
/// <summary>
/// 仓库名称
/// </summary>
public string Warehouse { get; set; }
/// <summary>
/// 日期
/// </summary>
public string Date { get; set; }
/// <summary>
/// 物料编号
/// </summary>
public string MaterialNumber { get; set; }
/// <summary>
/// 物料描述
/// </summary>
public string MaterialDesc { get; set; }
/// <summary>
/// 物料数量
/// </summary>
public string TotalNumber { get; set; }
/// <summary>
/// 物料单位
/// </summary>
public string Unit { get; set; }
/// <summary>
/// 制单人
/// </summary>
public string ReportMaker { get; set; }
/// <summary>
/// 物料存储人
/// </summary>
public string MaterialStoragePeople { get; set; }
/// <summary>
/// 审核人
/// </summary>
public string CheckerName { get; set; }
public WarehouseOrderRdlc(string titleName,string orderNumber,string department,string warehouse,string date,
string materialNumber,string materialDesc,string totalNumber,string unit,
string reportMaker,string materialStoragePeople,string checkerName)
{
this.TitleName = titleName;
this.OrderNumber = orderNumber;
this.Department = department;
this.Warehouse = warehouse;
this.Date = date;
this.MaterialNumber = materialNumber;
this.MaterialDesc = materialDesc;
this.TotalNumber = totalNumber;
this.Unit = unit;
this.ReportMaker = reportMaker;
this.MaterialStoragePeople = materialStoragePeople;
this.CheckerName = checkerName;
}
}
}//Class_end
}
|