unit U_ClientPrintRmf; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, RM_Common, RM_Class, RM_GridReport, RM_Dataset, RM_E_llPDF, RM_BarCode, RM_e_Graphic, RM_e_Jpeg, RM_e_Xls, cxContainer, cxMaskEdit, cxDropDownEdit, cxMRUEdit, dxSkinsCore, dxSkinsDefaultPainters, cxProgressBar, cxButtons, RM_System; type TfrmClientPrintRmf = class(TForm) ADOQueryTemp: TADOQuery; ADOConnection1: TADOConnection; ImageList1: TImageList; RMDB_1: TRMDBDataSet; RM1: TRMGridReport; RMDB_2: TRMDBDataSet; CDS_Label: TClientDataSet; ADO_1: TADOQuery; ADO_2: TADOQuery; RMDB_3: TRMDBDataSet; ADO_3: TADOQuery; RMXLSExport1: TRMXLSExport; RMJPEGExport1: TRMJPEGExport; RMBarCodeObject1: TRMBarCodeObject; ADOQueryCmd: TADOQuery; ADO_While: TADOQuery; ADO_4: TADOQuery; RMDB_4: TRMDBDataSet; ADO_5: TADOQuery; RMDB_5: TRMDBDataSet; ADOQueryReport: TADOQuery; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormShow(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure TV1DblClick(Sender: TObject); private IsDebug, FPreviewPrint, fIsPreview: Boolean; FLMType, FLBName: string; FExportFileType, FExportFileName: string; SqlStr1, SqlStr2, SqlStr3, SqlStr4, SqlStr5: string; FSuccessfulFun: string; FparamBlclid: string; procedure PrintReport(); procedure ExportReport(); procedure InitAdo(Ado: TADOQuery; SqlStr: string); procedure InitArgs(); { Private declarations } public FPrintJson: PChar; FJsonOut: PChar; constructor Create(AOwner: TComponent; JsonArgs: PChar); { Public declarations } end; var frmClientPrintRmf: TfrmClientPrintRmf; implementation uses U_DataLink, U_RTFun, superobject; {$R *.dfm} constructor TfrmClientPrintRmf.Create(AOwner: TComponent; JsonArgs: PChar); begin inherited Create(AOwner); FPrintJson := JsonArgs; end; procedure TfrmClientPrintRmf.InitArgs; var JSONObject, item: ISuperObject; jsonArray: TSuperArray; i, loopCount: Integer; begin // 解析 JSON 字符串 JSONObject := SO(FPrintJson); if JSONObject = nil then begin Application.MessageBox('PrintJson格式异常!', '提示', 0); Exit; end; // 提取基础字段 fIsPreview := JSONObject.B['IsPreview']; IsDebug := JSONObject.B['IsDebug']; FSuccessfulFun := JSONObject.S['SuccessfulFun']; FLMType := JSONObject.S['LMType']; FPreviewPrint := JSONObject.B['PreviewPrint']; FLBName := JSONObject.S['LBName']; FExportFileType := JSONObject.S['ExportFileType']; FExportFileName := JSONObject.S['ExportFileName']; // 设置默认导出文件名 if Trim(FExportFileName) = '' then FExportFileName := FLBName; // 导出文件操作(保持不变) ExportFtErpFile(FLBName + '.rmf', ADOQueryReport); // 初始化 SQL 字符串变量 SqlStr1 := ''; SqlStr2 := ''; SqlStr3 := ''; SqlStr4 := ''; SqlStr5 := ''; // 获取并处理 PrtArgs 数组 jsonArray := JSONObject.A['PrtArgs']; if Assigned(jsonArray) then begin // 确定循环次数(最多5个元素) loopCount := jsonArray.Length; for i := 0 to loopCount - 1 do begin item := jsonArray.O[i]; case i of 0: SqlStr1 := item.S['SqlStr']; 1: SqlStr2 := item.S['SqlStr']; 2: SqlStr3 := item.S['SqlStr']; 3: SqlStr4 := item.S['SqlStr']; 4: SqlStr5 := item.S['SqlStr']; end; end; end; // 设置报表预览按钮 // if FPreviewPrint then // RM1.PreviewButtons := [rmpbZoom, rmpbLoad, rmpbSave, rmpbPrint, rmpbFind, rmpbPageSetup, rmpbExit, rmpbSaveToXLS, rmpbExport, rmpbNavigator] // else // RM1.PreviewButtons := [rmpbZoom, rmpbLoad, rmpbSave, rmpbFind, rmpbPageSetup, rmpbExit, rmpbSaveToXLS, rmpbExport, rmpbNavigator]; end; procedure TfrmClientPrintRmf.InitAdo(Ado: TADOQuery; SqlStr: string); begin if Trim(SqlStr) = '' then Exit; with Ado do begin Close; sql.Clear; sql.add(SqlStr); // ShowMessage(SqlStr); Open; end; end; procedure TfrmClientPrintRmf.ExportReport(); var fPrintFile, fExportPath: string; begin fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLBName + '.rmf'; if FileExists(fPrintFile) then begin RM1.LoadFromFile(fPrintFile); RMVariables['LBPrtCode'] := dcode; RMVariables['LBPrtName'] := dname; fExportPath := ExtractFilePath(Application.ExeName) + FExportFileName + '.' + FExportFileType; if not DirectoryExists(ExtractFileDir(fExportPath)) then CreateDir(ExtractFileDir(fExportPath)); RM1.PrepareReport; //直接导出 // if FExportFileType = 'pdf' then // RM1.ExportTo(RMllPDFExport1, fExportPath) // else RM1.ExportTo(RMXLSExport1, fExportPath); FJsonOut := '{"success":true,"message":"导出成功!"}'; end else begin FJsonOut := '{"success":false,"message":"报表文件没找到!"}'; end; end; procedure TfrmClientPrintRmf.PrintReport(); var fPrintFile: string; begin fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLBName + '.rmf'; if FileExists(fPrintFile) then begin RM1.LoadFromFile(fPrintFile); // RM1.DefaultCopies := StrToIntDef(ComboBox1.Text, 1); RMVariables['LBPrtCode'] := dcode; RMVariables['LBPrtName'] := dname; if fIsPreview then RM1.ShowReport else RM1.PrintReport; FJsonOut := '{"success":true,"message":"打印成功!"}'; end else begin FJsonOut := '{"success":false,"message":"报表文件没找到!"}'; end; end; procedure TfrmClientPrintRmf.FormCreate(Sender: TObject); begin FPreviewPrint := True; try with ADOConnection1 do begin Connected := false; ConnectionString := DConString; Connected := true; end; except {if Application.MessageBox('网络连接失败,是否要再次连接?','提示',32+4)=IDYES then begin try with ADOConnection1 do begin Connected:=false; ConnectionString:=DConString; //ConnectionString:='23242'; Connected:=true; end; except end; end; } end; end; procedure TfrmClientPrintRmf.FormClose(Sender: TObject; var Action: TCloseAction); begin Action := cafree; end; procedure TfrmClientPrintRmf.FormShow(Sender: TObject); begin InitArgs(); if flbName = '' then begin FJsonOut := '{"success":false,"message":"未传入打印标签!"}'; self.Close; exit; end; if SqlStr1 = '' then begin FJsonOut := '{"success":false,"message":"未传入打印语句!"}'; self.Close; exit; end; InitAdo(ADO_1, SqlStr1); InitAdo(ADO_2, SqlStr2); InitAdo(ADO_3, SqlStr3); InitAdo(ADO_4, SqlStr4); InitAdo(ADO_5, SqlStr5); if FExportFileType = '' then begin PrintReport(); end else ExportReport(); self.Close; end; procedure TfrmClientPrintRmf.TV1DblClick(Sender: TObject); begin ModalResult := 1; end; procedure TfrmClientPrintRmf.FormDestroy(Sender: TObject); begin frmClientPrintRmf := nil; end; end.