题记:
最近有在用FastReport进行开发报表工作,当然也有在看书,突然想到可以用书中所写来实现一个fastreport的帮助类。
对于引用第三方类库,我们都会去将这个库在调用前进行相应的封装,也就是所谓的程序提供者模式。对第三方进行封装,当需要改变第三方(使用”第四方“的时候)不会影响到我们先前的实现,只需在程序提供处进行处理就好了。
好了,废话不多说,直接上些干活来:
一、定义基本的实现类
public class FastReportHelper { public FastReport.Report report = null; public FastReport.Report MReport { get { return report; } set { report = value; } } public FastReportHelper(string templateFile, string printerName, FastReport.Preview.PreviewControl pc) { if (string.IsNullOrEmpty(templateFile)) { throw new ArgumentNullException("报表模板不能为空!"); } if (!File.Exists(templateFile)) { throw new FileNotFoundException(string.Format("报表模板“{0}”不存在!", templateFile)); } if (!string.IsNullOrEmpty(printerName) && !PrinterInfo.ValidatePrinterIsExist(printerName)) { throw new Exception(string.Format("打印机“{0}”不存在!")); } if (pc != null) { report.Preview = pc; } report = new FastReport.Report(); report.Load(templateFile); } public void Show() { if (report != null) { report.Prepare(); report.ShowPrepared(); } } public void Print() { if (report != null) { report.PrintPrepared(); report.Print(); } } public void Print(PrinterSettings printSettings) { if (report != null) { this.report.PrintPrepared(printSettings); this.report.Print(); } } }
打印所需一般就分为:1、模板;2、数据源、3、打印设置,当然上面的类没有实现打印几份,单面打印还是双面打印等,所以均为默认。如果问我为毛这么写,那么请看下面
二、对基本的实现类进行扩展,从而在调用的时候实现链式编程。
public static class FastReportExtend { public static FastReportHelper RegesterData(this FastReportHelper fastReportHelper, DataSet ds) { for (int i = 0; i < ds.Tables.Count; i++) { fastReportHelper.MReport.RegisterData(ds.Tables[0], ds.Tables[0].TableName); } return fastReportHelper; } public static FastReportHelper RegesterData(this FastReportHelper fastReportHelper, IEnumerable<object> data, string dataName) { fastReportHelper.report.RegisterData(data, dataName); return fastReportHelper; } public static FastReportHelper RegesterData(this FastReportHelper fastReportHelper, DataTable dt, string dtName = "") { string name = string.IsNullOrEmpty(dtName) ? dt.TableName : dtName; fastReportHelper.report.RegisterData(dt, name); return fastReportHelper; } public static FastReportHelper SetParameters(this FastReportHelper fastReportHelper, Dictionary<string, object> dicParameters) { if (dicParameters != null || dicParameters.Count > 0) { foreach (KeyValuePair<string, object> item in dicParameters) { fastReportHelper.report.SetParameterValue(item.Key, item.Value); } } return fastReportHelper; } public static FastReport.Report SetParameters(this FastReport.Report report, Dictionary<string, object> dicParameters) { if (dicParameters != null || dicParameters.Count > 0) { foreach (KeyValuePair<string, object> item in dicParameters) { report.SetParameterValue(item.Key, item.Value); } } return report; } }
最后、打印机的获取
/// <summary> /// /// </summary> public class PrinterInfo { /// <summary> /// 判断打印机是否存在 /// </summary> /// <param name="printerName"></param> /// <returns></returns> public static bool ValidatePrinterIsExist(string printerName) { foreach (var item in PrinterSettings.InstalledPrinters) { if (item == printerName) return true; } return false; } /// <summary> /// 获取默认打印机名称 /// </summary> /// <returns></returns> public static string GetDefaultPrinter() { return new PrintDocument().PrinterSettings.PrinterName; } }
乱来一通,最近在搞vb.net,想屎的心都有了。
洗洗睡吧。
时间: 2024-10-05 03:49:12