Delphi Code - Insert/Create Table in Microsoft Excel to Organize and Analyze Data
Here is a simple Delphi procedure that creates a new Microsoft Excel file with one tab (i.e., one worksheet), adds some data to the worksheet, and inserts a table to organize and analyze the data. After these operations, the file is saved. The most relevant parts of the code are highlighted. Comments are included to explain the code. If you copy and paste the code into your program, be sure to change the form and procedure names to match your setup. For additional Excel automation solutions, see Microsoft Excel Automation with Delphi.
— share —
procedure TForm1.InsertTableToOrganizeAndAnalyzeData(Sender: TObject); var Row, Column: Integer; ExcelFileName, CellAddress: String; ExcelApplication, ExcelWorkbook, ExcelWorksheet: Variant; begin //be sure ComObj and Variants units are included in the "uses" clause
Row := 0; Column := 0; ExcelFileName := 'C:\PhysiologyWeb\delphi_code_examples\excel_file_insert_table.xlsx'; //replace file name with the name of your file CellAddress := '';
try //create Excel OLE ExcelApplication := CreateOleObject('Excel.Application'); except ExcelApplication := Null; //add error/exception handling code as desired end;
If VarIsNull(ExcelApplication) = False then begin try ExcelApplication.Visible := True; //set to False if you do not want to see the activity in the background ExcelApplication.DisplayAlerts := False; //ensures message dialogs do not interrupt the flow of your automation process. May be helpful to set to True during testing and debugging.
If VarIsNull(ExcelWorkbook) = False then begin //connect to Excel Worksheet using either the ExcelApplication or ExcelWorkbook handle try ExcelWorksheet := ExcelWorkbook.WorkSheets[1]; //[1] specifies the first worksheet //reference //https://docs.microsoft.com/en-us/office/vba/api/Excel.Worksheets.Item except ExcelWorksheet := Null; //add error/exception handling code as desired end;
If VarIsNull(ExcelWorksheet) = False then begin ExcelWorksheet.Select;
//add some data to worksheet For Row := 1to20do begin For Column := 1to20do begin CellAddress := ExcelWorksheet.Cells[Row,Column].Address; CellAddress := StringReplace(CellAddress, '$', '', [rfReplaceAll, rfIgnoreCase]); ExcelWorksheet.Cells[Row,Column] := CellAddress; end; end;
//insert table to organize and analyze the data ExcelWorksheet.Cells[1,1].Select; ExcelWorksheet.ListObjects.Add(1, EmptyParam, False, 2, EmptyParam, EmptyParam); //explanation of parameters //1 = xlSrcRange - specifies that data come from a range in the worksheet //EmptyParam tells Excell to pick the range automatically based on the location of the selected cell. $A$1:$T$20 can also be used in this case //False indicates that the data source is not external //2 = xlNo - specifies that the first row does not contains headers - this is unique to any given dataset //EmptyParam indicates no destination //EmptyParam tells Excel to pick the defaul table style
//table style can be specified //examples include TableStyleLight1, TableStyleLight20, TableStyleMedium5, TableStyleDark6, etc. //for no style (style = none) use: ExcelWorksheet.ListObjects['Table1'].TableStyle := '';