StableVersion4.3/HL_FristAidPlatform_Print/PrintByGridpp.cs

185 lines
7.8 KiB
C#
Raw Permalink Normal View History

2024-03-11 09:47:34 +08:00
using System;
using System.Data;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using gregn6Lib;
using HL_FristAidPlatform_Public;
namespace HL_FristAidPlatform_Print
{
/// <summary>
/// Grid++ Report报表打印
/// </summary>
public class PrintByGridpp
{
/// <summary>
/// 报表
/// </summary>
GridppReport Report = new GridppReport();
/// <summary>
/// 报表数据源
/// </summary>
DataTable ReportDT = new DataTable();
string PDFFileName;
/// <summary>
/// 设置默认打印机
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机
/// <summary>
/// 获取默认打印机
/// </summary>
/// <returns></returns>
public static string GetDefaultPrinter()
{
PrintDocument print = new PrintDocument();
string defaultName = print.PrinterSettings.PrinterName;//默认打印机名
return defaultName;
}
#region Grid++ Report报表打印
/// <summary>
/// 打印条形码
/// </summary>
/// <param name="_barCodeDT">条形码数据源</param>
/// <param name="_printerName">打印机名称</param>
/// <param name="_rptName">报表文件名称</param>
/// <param name="_isPreview">是否直接打印</param>
public string PrintReport(DataTable _barCodeDT, string _printerName, string _rptName, bool _isPreview)
{
string returnmsg = "";
if (_barCodeDT.Rows.Count > 0)
{
ReportDT = _barCodeDT;
//载入报表模板文件,必须保证 Grid++Report 的安装目录在C:\Program Files\Grid++Report 3
//关于动态设置报表路径与数据绑定参数请参考其它例子程序
Report.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "report\\" + _rptName);
//加载数据源
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
if (string.IsNullOrEmpty(_printerName))
{
_printerName = GetDefaultPrinter();
}
Report.Printer.PrinterName = _printerName;
if (_isPreview)
{
Report.Print(true);
}
else
{
Report.PrintPreview(false);
}
}
else
{
returnmsg = "报表数据源传输错误!";
}
return returnmsg;
}
/// <summary>
/// 打印PDF
/// </summary>
/// <param name="_barCodeDT"></param>
/// <param name="fileName"></param>
/// <param name="_rptName"></param>
/// <param name="GUID"></param>
/// <returns></returns>
public bool PrintReportToPDF(DataTable _barCodeDT, string fileName, string _rptName, string path, string PGUID)
{
if (_barCodeDT.Rows.Count > 0)
{
ReportDT = _barCodeDT;
//载入报表模板文件,必须保证 Grid++Report 的安装目录在C:\Program Files\Grid++Report 3
//关于动态设置报表路径与数据绑定参数请参考其它例子程序
Report.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "report\\" + _rptName);
//Report.ExportBegin += new _IGridppReportEvents_ExportBeginEventHandler(ReportExportBegin);
//加载数据源
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
GRExportType ExportType = GRExportType.gretPDF;
//直接调用ExportDirect方法执行导出任务
//Report.ExportDirect(ExportType, fileName, false, true);
//GridReportHelper.FillRecordToReport(Report, ReportDT);
PDFFileName = path + fileName;
string FileName = ExportInfo.GetExportFileName(PDFFileName);
//直接调用ExportDirect方法执行导出任务
Report.ExportDirect(ExportType, FileName, false, true);
PublicClass.UploadFile(FileName, PGUID);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 填充数据源
/// </summary>
private void ReportFetchRecord()
{
GridppReportUtility.FillRecordToReport(Report, ReportDT);
}
void ReportExportBegin(IGRExportOption Sender)
{
//ExportBegin 事件在将报表导出之前会触发到,无论是调用 ExportDirect 与 Export 方法,
//还是从打印预览窗口等地方执行导出,都会触发到 ExportBegin 事件。
//通常在 ExportBegin 事件中设置导出选项参数,改变默认导出行为
Sender.AbortOpenFile = true; //导出后不用关联程序打开导出文件如导出Excel文件之后不用Excel打开
Sender.AbortShowOptionDlg = true; //导出之前不显示导出选项设置对话框
//指定导出文件的完整路径与文件名称
string FileName = ExportInfo.GetExportFileName(PDFFileName);
Sender.FileName = FileName; //"d:\\export\\my.dat";
//根据导出类型设置其特有的选项参数,有关选项参数的具体信息清参考帮助文档。
//IGRExportOption是导出选项的基类其它具体导出选项的接口名称都以IGRE2为前缀
bool OnlyExportDetailGrid = false;//
bool SupressEmptyLines = false;//压缩空白行
switch (Sender.ExportType)
{
case GRExportType.gretXLS:
Sender.AsE2XLSOption.OnlyExportDetailGrid = OnlyExportDetailGrid;
Sender.AsE2XLSOption.SupressEmptyLines = SupressEmptyLines;
Sender.AsE2XLSOption.ExportPageHeaderFooter = false;
Sender.AsE2XLSOption.SameAsPrint = false;
Sender.AsE2XLSOption.ExportPageBreak = false;
break;
case GRExportType.gretRTF:
Sender.AsE2RTFOption.OnlyExportDetailGrid = OnlyExportDetailGrid;
Sender.AsE2RTFOption.SupressEmptyLines = SupressEmptyLines;
break;
case GRExportType.gretPDF:
Sender.AsE2PDFOption.Author = "My Author";
Sender.AsE2PDFOption.Subject = "My Subject";
break;
case GRExportType.gretHTM:
Sender.AsE2HTMOption.OnlyExportDetailGrid = OnlyExportDetailGrid;
Sender.AsE2HTMOption.SupressEmptyLines = SupressEmptyLines;
break;
case GRExportType.gretIMG:
Sender.AsE2IMGOption.DPI = 300;
Sender.AsE2IMGOption.ImageType = GRExportImageType.greitPNG;
break;
case GRExportType.gretTXT:
Sender.AsE2TXTOption.OnlyExportDetailGrid = OnlyExportDetailGrid;
Sender.AsE2TXTOption.SupressEmptyLines = SupressEmptyLines;
break;
default:
Sender.AsE2CSVOption.OnlyExportDetailGrid = OnlyExportDetailGrid;
Sender.AsE2CSVOption.SupressEmptyLines = SupressEmptyLines;
break;
}
}
#endregion
}
}