I now want to save the data from the designed Excel template. I opened the Excel template in the program, but the data details of the template need to be dynamically inserted, how to deal with? Excel Template:
title
Field 1 field 2 field 3 (detailed data display, format has been set) Total SUM (field 3)
date:
Handling program:
// This is the code I have revised, it is already, and everyone has procedure tform1.button5click (Sender: Tobject); Var Workbook, Sheet, Excelobj, Excel: Olevariant; RecordCounts, IPOS: Integer; i, J: Integer; Begin Excelobj: = CreateoleObject ('Excel.sheet'); Excel: = Excelobj.Application;
Excel.displayalerts: = false;
Workbook: = Excel.Workbooks.Open ('d: / assignment clear report template. Xls'); workbook.sheets [1] .name: = 'fund name'; Sheet: = Workbook.sheets [1]; Excel.activeWindow .Windowstate: = -4137;
// Sheet.cells [2,1] .value: = 'fund name'; Sheet.cells [3, 1] .value: = 'Date' Formator ('YYYY.MM.DD', DATE);
Recordcounts: = qry1.recordcount; // First cycle replicate data in the template Sheet.cells.Rows ['5: 5']. Select; for ipos: = 1 to Recordcounts do Begin
Sheet.cells.Rows ['5: 5']. Copy; Sheet.cells.Rows ['5: 5']. INSERT (-4121); // Insert Excel.cutcopyMode: = False; End before line 5 ;
// Enter the data into the template QRY1.First; J: = 5; While Not Qry1.eof do begin for i: = 1 to qry1.fieldcount do sheet.cells [J, I]: = TRIM (Qry1.fields [ I-1] .sstring; QRY1.NEXT; INC (J); END; QRY1.FIRST;
/ / Save Excel.ActiveWorkbook.saves ('D: / Test.xls'); workbook.close; Excel.quit; End;
The younger brother has an Excel template, wants to write data in the database to the template, but my code is always adding a sheet (Sheet4) in the open template, write the data to the new Sheet4 ( In that, what I want to have a template? How can I solve it? ? I want him to write in Sheet1! ! var ExcelApplication1: TExcelApplication; ExcelWorksheet1: TExcelWorksheet; ExcelWorkbook1: TExcelWorkbook; begin ExcelApplication1: = TExcelApplication.Create (Application); ExcelWorksheet1: = TExcelWorksheet.Create (Application); ExcelWorkbook1: = TExcelWorkbook.Create (Application); ExcelApplication1.Connect; excelapplication1. Visible [0]: = true; ExcelApplication1.Workbooks.Add ( 'C: / license database / original structure .xls', 0); ExcelWorkbook1.ConnectTo (ExcelApplication1.Workbooks [1]); ExcelWorksheet1.ConnectTo (ExcelWorkbook1.Worksheets [1] AS _Worksheet; Excelsheet.cells.Item [2,7]: = trim (bhy.text); // Write data excelsheet.printout; ================ ==================================================================================================================================================================================================================================================================== ? Answer1: Excelworksheet1.connectto (Excelworkbook1.activesheet as _Worksheet); Testing >> // By the way, in the end, if you don't save exit with ActiveWorkbook, WorkbookSheet? ? It seems to be setting ExcelWorkbook1.saved [0]: = true; if you do not prompt, you tried, remember the answer; uses comobj; Var Workbook, Sheet, Excelobj, Excel: olevariant; begin excelobj: = CreateoleObject ('Excel.Sheet'); Excel: = Excelobj.Application; Workbook: = Excel.Workbooks.Open ('C: / License Database / Original Structure .xls'); // Open Template Location: = Workbook . Sheets [1]; Excel.activeWindow.WindowsTate: = -4137; Excel.cells [2,7]: = trim (bhy.text);