Excel And D3
The following code is for Delphi 3 and Excel
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,
StdCtrls, Excel_TLB, ComObj, Db,
DBTables;
type
TMainFormF = class(TForm)
Button1: TButton;
tcustomer: TTable;
tcustomerCustNo: TFloatField;
tcustomerCompany:
TStringField;
tcustomerAddr1:
TStringField;
tcustomerAddr2:
TStringField;
tcustomerCity:
TStringField;
tcustomerState:
TStringField;
tcustomerZip:
TStringField;
tcustomerCountry:
TStringField;
tcustomerPhone:
TStringField;
tcustomerFAX:
TStringField;
tcustomerTaxRate:
TFloatField;
tcustomerContact:
TStringField;
tcustomerLastInvoiceDate:
TDateTimeField;
procedure Button1Click(Sender:
TObject);
procedure FormClose(Sender: TObject; var Action:
TCloseAction);
private
Excel :
Application;
ExcelBooks :
WorkBooks;
ExcelBook :
WorkBook;
ExcelSheet :
WorkSheet;
ExcelSheet1:
WorkSheet;
ExcelRange :
IRange;
LCID :
Integer;
ColumnRange: OleVariant;
tpRow: Integer;
//
tpCol: Integer; //
tpSaveChange :
OleVariant;
tpFileName :
OleVariant;
tpRoute : OleVariant;
Function SetUpLetter(pColCount :
Integer) : String;
public
{ Public declarations
}
end;
var
MainFormF: TMainFormF;
implementation
{$R *.DFM}
Function TMainFormF.SetUpLetter(pColCount : Integer) :
String;
Begin
Case pColCount Of
1
:
Result := 'A';
2
:
Result := 'B';
3
:
Result := 'C';
4
:
Result := 'D';
5
:
Result := 'E';
6
:
Result := 'F';
7
:
Result := 'G';
8
:
Result := 'H';
9
:
Result := 'I';
10
:
Result := 'J';
11
:
Result := 'K';
12
:
Result := 'L';
13
:
Result := 'M';
14
:
Result := 'N';
15
:
Result := 'O';
16
:
Result := 'P';
17
:
Result := 'Q';
18
:
Result := 'R';
19
:
Result := 'S';
20
:
Result := 'T';
21
:
Result := 'U';
22
:
Result := 'V';
23
:
Result := 'W';
24
:
Result := 'X';
25
:
Result := 'Y';
26
:
Result := 'Z';
27
:
Result := 'AA';
28
:
Result := 'AB';
29
:
Result := 'AC';
30
:
Result := 'AD';
31
:
Result := 'AE';
32
:
Result := 'AF';
33
:
Result := 'AG';
34
:
Result := 'AH';
35
:
Result := 'AI';
36
:
Result := 'AJ';
37
:
Result := 'AK';
38
:
Result := 'AL';
39
:
Result := 'AM';
40
:
Result := 'AN';
41
:
Result := 'AO';
42
:
Result := 'AP';
43
:
Result := 'AQ';
44
:
Result := 'AR';
45
:
Result := 'AS';
46
:
Result := 'AT';
47
:
Result := 'AU';
48
:
Result := 'AV';
49
:
Result := 'AW';
50
:
Result := 'AX';
51
:
Result := 'AY';
52
:
Result := 'AZ';
53
:
Result := 'BA';
54
:
Result := 'BB';
55
:
Result := 'BC';
56
:
Result := 'BD';
57
:
Result := 'BE';
58
:
Result := 'BF';
59
:
Result := 'BG';
60
:
Result := 'BH';
61
:
Result := 'BI';
62
:
Result := 'BJ';
63
:
Result := 'BK';
64
:
Result := 'BL';
65
:
Result := 'BM';
66
:
Result := 'BN';
67
:
Result := 'BO';
68
:
Result := 'BP';
69
:
Result := 'BQ';
End;
End;
procedure TMainFormF.FormClose(Sender: TObject; var Action:
TCloseAction);
begin
Try
tpSaveChange := False;
tpFileName
:= False;
tpRoute :=
False;
ExcelBook.Close(tpSaveChange,
tpFileName, tpRoute, Lcid);
EXCEL.Quit;
Except
End;
end;
procedure TMainFormF.Button1Click(Sender:
TObject);
Var
tpColName :
String;
tpColCount : integer;
tpCellName : String;
tpCellCount : Integer;
Begin
tpCellCount := 1;
Excel :=
CoApplication.Create;
LCID :=
GetUserDefaultLCID;
Try
Excel.Visible[LCID] := true;
// blank
workbook
Excelbook :=
Excel.Workbooks.Add(xlWBATWorksheet, LCID);
tpCellCount :=
2;
tpColCount := 1;
With tcustomer
Do
Begin
Open;
First;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'CustNo';
Excel.Range[tpColName,
tpColName].ColumnWidth := 8;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Company';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Addr1';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Addr2';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'City';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'State';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Zip';
Excel.Range[tpColName,
tpColName].ColumnWidth := 16;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Country';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Phone';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Fax';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'TaxRate';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'Contact';
Excel.Range[tpColName,
tpColName].ColumnWidth := 30;
tpColCount
:= tpColCount + 1;
tpColName
:= SetUpLetter(tpColCount) +
IntToStr(1);
EXCEL.Range[tpColName, tpColName].Value :=
'LastInvoiceDate';
Excel.Range[tpColName, tpColName].ColumnWidth :=
30;
tpColCount
:= tpColCount + 1;
While Not EOF
Do
Begin
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerCustNo.AsInteger;
tpColCount := tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerCompany.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerAddr1.AsString;;
tpColCount := tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerAddr2.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerCity.AsString;;
tpColCount := tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerState.AsString;;
tpColCount := tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerZip.AsString;;
tpColCount := tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerCountry.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerPhone.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerFax.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerTaxRate.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerContact.AsString;
tpColCount
:= tpColCount + 1;
tpCellName
:= SetUpLetter(tpColCount) +
IntToStr(tpCellCount);
EXCEL.Range[tpCellName, tpCellName].Value :=
tcustomerLastInvoiceDate.AsDateTime;
tpColCount
:= tpColCount + 1;
tpColCount := 1;
tpCellCount := tpCellCount +
1;
Next;
End;
End;
Finally
End;
end;