Export Excel from DBGRID, StringGrid

xiaoxiao2021-03-05  19

procedure TDM.CopyDbDataToExcel (Target: TDBGridEh; mb, FileName: string); variCount, jCount: Integer; XLApp: Variant; Sheet: Variant; beginScreen.Cursor: = crHourGlass; if not VarIsEmpty (XLApp) thenbegin XLApp.DisplayAlerts: = False ; Xlapp.quit; varclear (xlapp); end; // -------------------------- Select Template ------ // CopyFile (Pchar (Trim (Application.exename) MB '. Xls'), Pchar (FileName '1.xls'), False); // -------------- ---------- // Create an Excel object through OLE XLAPP: = CreateoleObject ('Excel.Application'); Excel.Application '); Except Screen.cursor: = crdefault; EXIT; End; if MB =' Statistics - Project Information Front 'THEN XLAPP.WORKS.ADD [TRIM (Application.exename) MB '. Xls'] // You have to put the data there, Mr. Cheng file in E: /1.XLSELSE XLAPP.Workbooks .Add; xlapp.workbooks [1] .worksheets [1] .name: = 'Sheet1'; Sheet: = xlapp.workbooks [1] .Worksheets ['Sheet1']; if NOTT.ACTIVE THENBEGIN SCREEN .Cursor: = crdefault; exit; end; target.datasource.dataset.first;

For iCount: = 0 to target.columns.count - 1 dobegin Sheet.cells [1, ICOUNT 1]: = Trim (target.columns.Items [iCount] .title.caption);

JCOUNT: = 1; While Not Target.DataSource.Dataset.eof Dobegin

For iCount: = 0 to target.columns.count - 1 do // begin if Icount = 0 Then Sheet.cells [JCOUNT 1, ICOUNT 1]: = INTTOSTR (JCOUNT) Else Sheet.cells [JCOUNT 1, ICOUNT 1]: = Trim (target.columns.Items [iCount] .field.Asstring); end; inc (jcount); target.datasource.dataset.next; end; // ------------ ---------- Can add Excel's macro here -----------

XLapp.activeWorkbook.saveas (filename: = filename); screen.cursor: = crdefault; xlapp.activeworkbook.close; end; -------------------------------------------------------------------------------------------------- ------------------ StringGridtoExcel

procedure TDM.CopySGDataToExcel (Target: TStringGrid; FileName: string); variCount, jCount: Integer; XLApp: Variant; Sheet: Variant; beginScreen.Cursor: = crHourGlass; if not VarIsEmpty (XLApp) thenbeginXLApp.DisplayAlerts: = False; XLApp. Quit; varclear (xlapp); end; // Create an Excel object through OLE Tryxlapp: = CreateoleObject ('Excel.Application'); Exceptscreen.cursor: = crdefault; exit; end; xlapp.workbooks.add; // You have to put The data is placed there, Mr. Cheng file is in E: /1.xlsxlapp.workbooks [1] .Worksheets [1] .Name: = 'Sheet1'; Sheet: = xlapp.workbooks [1] .Worksheets ['Sheet1' ]; Target.row: = 0; for iCount: = 0 to target.rowcount - 1 dobegin for jcount: = 0 to target.colcount - 1 do sheet.cells [iCount 1, jcount 1]: = target.cells [jcount, iCount]; target.row: = iCount; end; xlapp.activeworkbook.saveas (filename: = filename); screen.cursor: = crdefault; xlapp.activeworkbook.close;

转载请注明原文地址:https://www.9cbs.com/read-38850.html

New Post(0)