procedure ToExce(table1:Tdataset); var WorkBk : _WorkBook; // Define a WorkBook WorkSheet : _WorkSheet; // Define a WorkSheet I, J, R, C : Integer; IIndex : OleVariant; TabGrid : Variant; XLapp:TexcelApplication; begin XLapp:=TexcelApplication.create(nil); IIndex := 1; R := table1.RecordCount; C := table1.FieldCount; // Create the Variant Array TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr); I := 0; // Define the loop for filling in the Variant table1.First; while not table1.Eof do begin for J := 0 to (C - 1) do TabGrid[I,J] := table1.Fields[J].Asstring ; Inc(I,1); table1.Next ; end; // Connect to the server TExcelApplication XLApp.Connect; // Add WorkBooks to the ExcelApplication XLApp.WorkBooks.Add(xlWBatWorkSheet,0); // Select the first WorkBook WorkBk := XLApp.WorkBooks.Item[IIndex]; // Define the first WorkSheet WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet; // Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value := TabGrid; // Customise the WorkSheet WorkSheet.Name := 'Customers'; Worksheet.Columns.Font.Bold := True; Worksheet.Columns.HorizontalAlignment := xlRight; WorkSheet.Columns.ColumnWidth := 14; // Customise the first entire Column //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue; //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft; //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31; // Show Excel XLApp.Visible[0] := True; // Disconnect the Server XLApp.Disconnect; // Unassign the Delphi Variant Matrix TabGrid := Unassigned; end; end.
var
WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
XLapp:TexcelApplication;
begin
XLapp:=TexcelApplication.create(nil);
IIndex := 1;
R := table1.RecordCount;
C := table1.FieldCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr); I := 0;
// Define the loop for filling in the Variant
table1.First;
while not table1.Eof do
begin
for J := 0 to (C - 1) do
TabGrid[I,J] := table1.Fields[J].Asstring ;
Inc(I,1);
table1.Next ;
end; // Connect to the server TExcelApplication
XLApp.Connect;
// Add WorkBooks to the ExcelApplication
XLApp.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := XLApp.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value := TabGrid;
// Customise the WorkSheet
WorkSheet.Name := 'Customers';
Worksheet.Columns.Font.Bold := True;
Worksheet.Columns.HorizontalAlignment := xlRight;
WorkSheet.Columns.ColumnWidth := 14;
// Customise the first entire Column
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
// Show Excel
XLApp.Visible[0] := True;
// Disconnect the Server
XLApp.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end;
end.