[C#] Winform - FastReport(开发篇)

一、设计报表

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”按钮后,自动查看导出的文件:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值