uses
ComObj;
var
ExcelApp: OleVariant;
implementation
procedure
TForm1
.
Button1Click(Sender: TObject);
const
xlChart = -
4109
;
xlWorksheet = -
4167
;
xlWBATWorksheet = -
4167
;
xlWBATChart = -
4109
;
xlPortrait =
1
;
xlLandscape =
2
;
xlPaperA4 =
9
;
xlBottom = -
4107
;
xlLeft = -
4131
;
xlRight = -
4152
;
xlTop = -
4160
;
xlHAlignCenter = -
4108
;
xlVAlignCenter = -
4108
;
xlThick =
4
;
xlThin =
2
;
var
ColumnRange: OleVariant;
function
GetLastLine(AColumn:
Integer
):
Integer
;
const
xlUp =
3
;
begin
Result := OLEConTainer1
.
OleObject
.
Range[
Char
(
96
+ AColumn) +
IntToStr(
65536
)].
end
[xlUp].Rows
.
Row;
end
;
begin
try
ExcelApp := GetActiveOleObject(
'Excel.Application'
);
except
try
ExcelApp := CreateOleObject(
'Excel.Application'
);
except
ShowMessage(
'Cannot start Excel/Excel not installed ?'
);
Exit;
end
;
end
;
ExcelApp
.
Workbooks
.
Add(xlWBatWorkSheet);
ExcelApp
.
Workbooks
.
Open(
'c:\YourFileName.xls'
);
ExcelApp
.
ActiveSheet
.
Name :=
'This is Sheet 1'
;
ExcelApp
.
Workbooks[
1
].WorkSheets[
1
].Name :=
'This is Sheet 1'
;
ExcelApp
.
Cells[
1
,
1
].Value :=
'SwissDelphiCenter.ch'
;
ExcelApp
.
Cells[
3
,
1
].Value := FormatDateTime(
'dd-mmm-yyyy'
, Now);
ExcelApp
.
Range[
'A2'
,
'D2'
].Value := VarArrayOf([
1
,
10
,
100
,
1000
]);
ExcelApp
.
Range[
'A11'
,
'A11'
].Formula :=
'=Sum(A1:A10)'
;
ExcelApp
.
Cells[
2
,
1
].HorizontalAlignment := xlright;
ColumnRange := ExcelApp
.
Workbooks[
1
].WorkSheets[
1
].Columns;
ColumnRange
.
Columns[
1
].ColumnWidth :=
20
;
ColumnRange
.
Columns[
2
].ColumnWidth :=
40
;
ExcelApp
.
Rows[
1
].RowHeight :=
15.75
;
ExcelApp
.
Range[
'B3:D3'
].Mergecells :=
True
;
ExcelApp
.
Range[
'A14:M14'
].Borders
.
Weight := xlThick;
ExcelApp
.
Range[
'A14:M14'
].Borders
.
Weight := xlThin;
ExcelApp
.
Range[
'B16:M26'
].Font
.
Bold :=
True
;
ExcelApp
.
Range[
'B16:M26'
].Font
.
Size :=
12
;
ExcelApp
.
Cells[
9
,
6
].HorizontalAlignment := xlright;
ExcelApp
.
Range[
'B14:M26'
].HorizontalAlignment := xlHAlignCenter;
ExcelApp
.
Range[
'B14:M26'
].VerticallyAlignment := xlVAlignCenter;
ExcelApp
.
ActiveSheet
.
PageSetup
.
Orientation := xlLandscape;
ExcelApp
.
ActiveSheet
.
PageSetup
.
LeftMargin :=
35
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
RightMargin := -
15
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
FooterMargin := ExcelApp
.
InchesToPoints(
0
);
ExcelApp
.
ActiveSheet
.
PageSetup
.
FitToPagesWide :=
1
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
FitToPagesTall :=
3
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
Zoom :=
95
;
ExcelApp
.
PageSetup
.
PaperSize := xlPaperA4;
ExcelApp
.
ActiveWindow
.
DisplayGridlines :=
False
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
BlackAndWhite :=
False
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
RightFooter :=
'Right Footer / Rechte Fu?zeile'
;
ExcelApp
.
ActiveSheet
.
PageSetup
.
LeftFooter :=
'Left Footer / Linke Fu?zeile'
;
ShowMessage(Format(
'Excel Version %s: '
, [ExcelApp
.
Version]));
ExcelApp
.
Visible :=
True
;
ExcelApp
.
SaveAs(
'c:\filename.xls'
);
ExcelApp
.
ActiveWorkBook
.
SaveAs(
'c:\filename.xls'
);
end
;