unigui導出TMS.Flexcel【5】
阿新 • • 發佈:2018-08-13
eve sig exceptio dst time 創建 hid pro session
procedure TUniFrmeWebEmbedBase.ExportData; //導出到excel var FlexCelImport1: TExcelFile; i, rowindex, colindex: Integer; ms : TMemoryStream; begin { 檢查數據集 } if Assigned(cdsMain) and cdsMain.Active then try { 創建Excel文件 } FlexCelImport1 := TXlsFile.Create(true);//NewFile(1);View CodeFlexCelImport1.NewFile(1); // FlexCelImport1.SupportsXlsx := True; // FlexCelImport1.NewFile(WorkSheetCount, TExcelFileFormat(2) ); //2 = v2010, 1 = v2007, 0 = v2003 // FlexCelImport1.ActiveSheet := 1; cdsMain.First; { 第一行為標題 } rowindex := 1; colindex := 1; for i := 0 to cdsMain.FieldCount -1do { 只導出數據集中可視的字段 } if cdsMain.Fields[i].Visible then begin FlexCelImport1.SetCellValue(rowindex, colindex,cdsMain.Fields[i].DisplayLabel); inc(colindex); end; { 依次導出數據 } while not cdsMain.Eof do begin { 增加一行 } inc(rowindex); colindex := 1; for i := 0 to cdsMain.FieldCount -1 do { 只導出數據集中可視的字段 } if cdsMain.Fields[i].Visible then begin { 時間和日期字段需要單獨出來一下,否則出來的都是浮點數 } if cdsMain.Fields[i].DataType in [ftDate, ftDateTime, ftTime, ftTimeStamp] then FlexCelImport1.SetCellValue(rowindex, colindex, cdsMain.Fields[i].AsString) else FlexCelImport1.SetCellValue(rowindex, colindex, cdsMain.Fields[i].Value); inc(colindex); end; { 下一條 } cdsMain.Next; end; FlexCelImport1.PrintLandscape := true; // FlexCelImport1. { 創建內存流 } ms := TMemoryStream.Create; try { 將Excel保存到內存流 } FlexCelImport1.save(ms); ms.Position := 0; { 將數據流發送到客戶端,同時指定下載文件名,非常簡單 } UniSession.SendStream(ms, ‘usegear.xls‘ ); finally ms.Free; { 清理內存 } FlexCelImport1.Free; end; except on E : Exception do FlexCelImport1.Free; end; end;
調用
procedure TUniFrmeWebEmbedBase_authoManage.act_ExportExecute(Sender: TObject); begin cdsMain.Data := Unit_dm.DataModule1.getTabeldata(‘select * from SM_role‘) ; inherited; end;
unigui導出TMS.Flexcel【5】