/****************************** * 说明:表格常规打印 * 创建人:龚宇超 * 创建日期:2017-11-10 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraPrinting; using DevExpress.XtraGrid; using DevExpress.XtraReports.UI; using System.Data; using Lskj.Business.Impl; using Lskj.Util; using DevExpress.LookAndFeel; using DevExpress.XtraReports.UserDesigner; using Lskj.Model; using Lskj.Data; using System.IO; using FastReport; using FastReport.Preview; using System.Windows.Forms; using System.Threading.Tasks; using Lskj.Business; namespace Lskj.Control.Model { /// /// 表格常规打印 /// public class PrintUtil { /// /// 打印模版后缀 /// public static string TemplateFlag = ".frx"; /// /// 打印完成后事件 /// public static EventHandler OnAfterPrint; public static void Print(GridControl gridControl) { try { PrintingSystem ps = null; DevExpress.XtraPrinting.PrintableComponentLink link = null; ps = new DevExpress.XtraPrinting.PrintingSystem(); link = new DevExpress.XtraPrinting.PrintableComponentLink(ps); ps.Links.Add(link); link.Component = gridControl;//这里可以是可打印的部件 string _PrintHeader = "打印数据信息"; PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; phf.Header.Content.Clear(); phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" }); phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold); phf.Header.LineAlignment = BrickAlignment.Center; link.CreateDocument(); //建立文档 ps.PreviewFormEx.Show(); } catch (Exception ex) { //MessageUtil.Show(ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:模版打印 /// 创建人:龚宇超 /// 创建日期:2018-06-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The SQLS. /// The print file. /// The print file. /// The print file. public static void FastReportPrint(List sqls, string printFile, bool isPrint, string printerName = "", List> dataTableTasks = null) { Report report = new Report(); FrmFastReportDesign design = new FrmFastReportDesign(); try { WaitForm.ShowForm(ResourceKeys.Handing); FastReport.Utils.Res.LoadLocale(PubUtil.PrintModeResourcePath); string path = PubUtil.PrintModePath + printFile; if (File.Exists(path) && !string.IsNullOrEmpty(printFile)) report.Load(path); design.Text = Path.GetFileName(printFile); design.ReportObj = report; if (dataTableTasks != null && dataTableTasks.Count > 0) { design.SetDataSource(dataTableTasks); } else { design.SetDataSource(sqls.ToArray()); } if (isPrint && printerName != null) { design.ReportObj.PrintSettings.Printer = printerName; design.ReportObj.PrintSettings.ShowDialog = false; design.ReportObj.Print(); if (OnAfterPrint != null) OnAfterPrint(null, null); } else if (ERPInfo.Instance.IsUserManager|| SystemInfo.Instance.PrintTemplate.Split(',').Contains(ERPInfo.Instance.UserName)) { using (BaseForm form = new BaseForm()) { form.Text = "打印程序"; form.IsMdiContainer = true; form.WindowState = System.Windows.Forms.FormWindowState.Maximized; try { design.ReportObj.Prepare(); design.ReportObj.Design(form); design.ReportObj.Designer.BorderStyle = BorderStyle.None; design.ReportObj.Designer.cmdPreview.CustomAction += OnCustomAction; form.ShowDialog(); } finally { ReleaseFastReportDesignerResource(design); } } } else { using (BaseForm form = new BaseForm()) { form.Text = "打印程序"; form.IsMdiContainer = true; form.WindowState = System.Windows.Forms.FormWindowState.Maximized; design.ReportObj.Prepare(); design.ReportObj.ShowPrepared(form); design.ReportObj.Preview.OnPrint += Preview_OnPrint; design.ReportObj.Preview.ParentForm.WindowState = FormWindowState.Maximized; form.ShowDialog(); } } WaitForm.HideForm(); //design.ShowDialog(); } catch (Exception ex) { //MessageUtil.Show(ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); if (design != null && design.ReportObj != null) { design.ReportObj.Design(); } WaitForm.HideForm(); } } private static void ReleaseFastReportDesignerResource(FrmFastReportDesign design) { try { if (design != null && design.ReportObj != null && design.ReportObj.Designer != null) { try { design.ReportObj.Designer.StopAutoSave(); } catch { } try { design.ReportObj.Designer.cmdPreview.CustomAction -= OnCustomAction; } catch { } } if (design != null && design.ReportObj != null) { design.ReportObj.Dispose(); design.ReportObj = null; } } catch { } } private static void OnCustomAction(object sender, EventArgs e) { FastReport.Design.StandardDesigner.DesignerControl design = sender as FastReport.Design.StandardDesigner.DesignerControl; FastReport.Report report = design.Report; System.Windows.Forms.Form form = new System.Windows.Forms.Form(); form.IsMdiContainer = true; form.WindowState = System.Windows.Forms.FormWindowState.Maximized; report.Prepare(); report.ShowPrepared(form); report.Preview.OnPrint += Preview_OnPrint; report.Preview.ParentForm.WindowState = FormWindowState.Maximized; form.ShowDialog(); report.Preview = null; form.Dispose(); } private static void Preview_OnPrint(object sender, PreviewControl.PrintEventArgs e) { try { if (e.Preview != null && e.Preview.Report != null && e.Preview.Report.Preview != null) { //this.ReportObj.PrintSettings.PageRange = PageRange.All; // 设置为全部页面 //this.ReportObj.PrintSettings.PageNumbers = ""; // 清空页码范围 bool isPrint = e.Preview.Report.Preview.Print(); if (isPrint) { if (OnAfterPrint != null) OnAfterPrint(sender, e); } } else { MessageUtil.Show("Preview, Report or Report Preview is null"); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } } }