一、设计报表
1、创建数据源和参数:
2、设计页面:
3、编写代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;
namespace FastReport
{
public class ReportScript
{
private void _StartReport(object sender, EventArgs e)
{
Picture1.ImageLocation = (string)Report.GetParameterValue("meiPath");
Picture2.ImageLocation = (string)Report.GetParameterValue("lanPath");
Picture3.ImageLocation = (string)Report.GetParameterValue("zhuPath");
Picture4.ImageLocation = (string)Report.GetParameterValue("juPath");
}
private void Table1_ManualBuild(object sender, EventArgs e)
{
DataSourceBase ds = Report.GetDataSource("flower");
ds.Init();
Table1.PrintRow(0);
Table1.PrintColumns();
while(ds.HasMoreRows)
{
Table1.PrintRow(1);
Table1.PrintColumns();
ds.Next();
}
}
}
}
二、创建项目
1、Form1.cs 设计器:
2、Form1.cs 代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp.StudyFastReport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExportPdf_Click(object sender, EventArgs e)
{
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Pdfs", $"{DateTime.Now:yyyyMMddHHmmss}.pdf");
string reportPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reports", "FlowerReport.frx");
FastReportHelper.ExportPdf(pdfPath, reportPath, GetReportParameters(), GetReportDataSet());
Process.Start(pdfPath);
}
private Dictionary<string, object> GetReportParameters()
{
string meiPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "mei.png");
string lanPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "lan.png");
string zhuPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "zhu.png");
string juPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "ju.png");
Dictionary<string, object> dict = new Dictionary<string, object>
{
{ "documentTitle", "花四君子" },
{ "meiPath", meiPath },
{ "lanPath", lanPath },
{ "zhuPath", zhuPath },
{ "juPath", juPath }
};
return dict;
}
private DataSet GetReportDataSet()
{
DataTable dt = new DataTable("FlowerTable");
dt.Columns.Add("name");
dt.Columns.Add("moral");
dt.Rows.Add("梅", "傲霜凌寒");
dt.Rows.Add("兰", "谦谦君子");
dt.Rows.Add("竹", "高雅之士");
dt.Rows.Add("菊", "傲然不屈");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
}
}
3、FastReportHelper.cs :
using FastReport;
using FastReport.Export.Pdf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApp.StudyFastReport
{
public class FastReportHelper
{
public static void ExportPdf(string pdfPath, string reportPath, Dictionary<string, object> parameters, DataSet dataSet)
{
// create report instance
Report report = new Report();
try
{
// load the existing report
report.Load(reportPath);
// set parameters
if (parameters != null)
{
foreach (var item in parameters)
{
report.SetParameterValue(item.Key, item.Value);
}
}
// register the dataset
report.RegisterData(dataSet);
// prepare the report
report.Prepare();
// export to pdf
PDFExport export = new PDFExport();
export.EmbeddingFonts = true;
report.Export(export, pdfPath);
// export to image
//ImageExport image = new ImageExport();
//image.ImageFormat = ImageExportFormat.Jpeg;
//report.Export(image, "");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
report.Dispose();
}
}
}
}
三、预览
1、运行:
2、点击“Export Pdf”按钮后,自动查看导出的文件: