在Nuget程序包中搜索FastReport.Net.Demo并安装。
//模板路径
string bReportPath = Environment.CurrentDirectory + $@"模板.frx";
Report bReport;
if (File.Exists(bReportPath))
{
bReport = Report.FromFile(bReportPath);
}
else
{
bReport = new()
{
FileName = bReportPath
};
}
//模拟的数据结构
DataSet bDs = new DataSet();
DataTable bTbl = new DataTable();
bTbl.Columns.Add("数值1", typeof(string));
bTbl.Columns.Add("数值2", typeof(string));
bTbl.Columns.Add("数值3", typeof(string));
bDs.Tables.Add(bTbl);
//加载数据
bReport.RegisterData(bDs);
//添加数据源
bReport.GetDataSource(bDs.Tables[0].TableName).Enabled = true;
//开始设计
bReport.Design(true);
//测试数据
DataSet bDs = new DataSet();
DataTable bTbl = new DataTable();
bTbl.Columns.Add("数值1", typeof(string));
bTbl.Columns.Add("数值2", typeof(string));
bTbl.Columns.Add("数值3", typeof(string));
//准备测试数据
for (int i = 0;i < 10;i++) {
DataRow bDr = bTbl.NewRow();
bDr[0] = i + 10;
bDr[0] = i + 11;
bDr[0] = i + 12;
bTbl.Rows.Add(bDr);
}
bDs.Tables.Add(bTbl);
//测试文件路径
string bFilePath = Environment.CurrentDirectory + $@"模板.xlsx";
string bReportPath = Environment.CurrentDirectory + $@"模板.frx";
//
Report bReport = Report.FromFile(bReportPath);
//加载数据
bReport.RegisterData(bDs);
//准备报告,不做准备的话,没有数据
bReport.Prepare();
bReport.Export(new FastReport.Export.OoXML.Excel2007Export(), bFilePath);