using FastReport; using FastReport.Data; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace formula_manage.UserClass { public class PrintHelper { /// /// 打印 /// /// 打印机 /// 模板 /// 字典参数 /// 数据源 /// 打印数量 /// public static Tuple Print(string printerName, string frxPath, Dictionary dicParam, DataSet dsDataSource, int printNum = 1) { bool flag = false; string msg = ""; FastReport.Report report = new FastReport.Report(); try { report.Load(frxPath); report.DoublePass = true; if (dicParam != null && dicParam.Count > 0) { foreach (var item in dicParam) { report.SetParameterValue(item.Key, item.Value); } } if (dsDataSource != null && dsDataSource.Tables.Count > 0) { report.RegisterData(dsDataSource); foreach (DataSourceBase dataSourceBase in report.Dictionary.DataSources) { dataSourceBase.Enabled = true; } } report.PrintSettings.ShowDialog = false; report.PrintSettings.Printer = printerName; report.PrintSettings.PrintMode = PrintMode.Split; EnvironmentSettings envSet = new EnvironmentSettings(); envSet.ReportSettings.ShowProgress = false; for (int i = 0; i < printNum; i++) { report.Print(); } flag = true; msg = "打印成功"; } catch (Exception ex) { flag = false; msg = ex.Message; } finally { report.Dispose(); } return new Tuple(flag, msg); } /// /// 设计 /// /// 模板 /// public static Tuple Design(string frxPath) { bool flag = false; string msg = ""; FastReport.Report report = new FastReport.Report(); try { string chineseSimpleFrl = AppDomain.CurrentDomain.BaseDirectory + @"FastReport\Localization\language.frl"; FastReport.Utils.Res.LoadLocale(chineseSimpleFrl); } catch (Exception ex) { } try { report.Load(frxPath); report.DoublePass = true; report.Design(); flag = true; msg = "Designer Open"; } catch (Exception ex) { flag = false; msg = ex.Message; } finally { report.Dispose(); } return new Tuple(flag, msg); } /// /// 设计 /// /// 模板 /// 字典参数 /// 数据源 /// public static Tuple Designs(string frxPath, Dictionary dicParam, DataSet dsDataSource) { bool flag = false; string msg = ""; FastReport.Report report = new FastReport.Report(); try { report.Load(frxPath); report.DoublePass = true; if (dicParam != null && dicParam.Count > 0) { foreach (var item in dicParam) { report.SetParameterValue(item.Key, item.Value); } } if (dsDataSource != null && dsDataSource.Tables.Count > 0) { report.RegisterData(dsDataSource); foreach (DataSourceBase dataSourceBase in report.Dictionary.DataSources) { dataSourceBase.Enabled = true; } } report.Design(); flag = true; msg = "Designer Open"; } catch (Exception ex) { flag = false; msg = ex.Message; } finally { report.Dispose(); } return new Tuple(flag, msg); } } }