Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
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 := '';

ExcelApplication := Null;
ExcelWorkbook := Null;
ExcelWorksheet := Null;

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.

//Open Excel Workbook
try
ExcelWorkbook := ExcelApplication.WorkBooks.Add(-4167);
//or
//ExcelWorkbook := ExcelApplication.WorkBooks.Add;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.workbooks.add
except
ExcelWorkbook := Null;
//add error/exception handling code as desired
end;

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 := 1 to 20 do
begin
For Column := 1 to 20 do
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 := '';

//references
//https://learn.microsoft.com/en-us/office/vba/api/Excel.ListObjects.Add
//https://learn.microsoft.com/en-us/office/vba/api/excel.xllistobjectsourcetype
//https://learn.microsoft.com/en-us/office/vba/api/excel.listobject
//https://learn.microsoft.com/en-us/office/vba/api/excel.tablestyle
//https://learn.microsoft.com/en-us/office/vba/api/excel.xlyesnoguess
end;

//save the workbook
ExcelWorkbook.SaveAs(ExcelFileName);
end;
finally
ExcelApplication.Workbooks.Close;
ExcelApplication.DisplayAlerts := True;
ExcelApplication.Quit;

ExcelWorksheet := Unassigned;
ExcelWorkbook := Unassigned;
ExcelApplication := Unassigned;
end;
end;
end;




Posted: Sunday, January 14, 2024
Last updated: Wednesday, March 19, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL