unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExcelXP;
type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
const Testname = '
Test.xls';
var
Form1: TForm1;
FExcelApplication: ExcelApplication;
FExcelWorkbook: ExcelWorkbook;
FExcelWorksheet: ExcelWorksheet;
sparentroot:
string;
iUserLCID: integer;
implementation
{$R *.dfm}
procedure ExcelDatenbank_anlegen;
begin
FExcelApplication := CoExcelApplication.Create;
FExcelApplication.Workbooks.Add(emptyparam, iUserLCID);
FExcelWorkbook := FExcelApplication.ActiveWorkbook;
FExcelWorkbook.Worksheets.Add(emptyParam, emptyParam, emptyParam, emptyParam, iUserLCID);
FExcelWorksheet := FExcelWorkbook.ActiveSheet
as ExcelWorksheet;
FExcelWorksheet.Cells.Item[3,1] := '
Hallo';
FExcelWorksheet.Cells.Item[3,2] := '
dies';
FExcelWorksheet.Cells.Item[3,3] := '
ist';
FExcelWorksheet.Cells.Item[3,4] := '
ein';
FExcelWorksheet.Cells.Item[3,5] := '
Test';
FExcelApplication.visible[iUserLCID] := true;
end;
procedure ExcelDatenbank_oeffnen;
begin
FExcelApplication := CoExcelApplication.Create;
FExcelApplication.Workbooks.Open(ExtractFilePath(ParamStr(0)) + TestName, emptyParam, emptyParam, emptyParam, emptyParam
, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam
, emptyParam, emptyParam, iUserLCID);
FExcelApplication.visible[iUserLCID] := true;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
// Speichern und Schließen von Excel
FExcelApplication.ActiveWorkbook.Close(true, sparentroot + Testname, emptyParam, iUserLCID);
end;
procedure TForm1.FormCreate(Sender: TObject);
var DWResult: DWORD;
begin
sparentroot := ExtractFilePath(ParamStr(0));
iUserLCID := GetUserDefaultLCID;
if FindWindow('
XLMain','
Microsoft Excel - ' + TestName) <> 0
then
SendMessageTimeout(FindWindow('
XLMain','
Microsoft Excel - ' + TestName), WM_CLOSE, 0, 0,
SMTO_ABORTIFHUNG
or SMTO_NORMAL, 5000, DWResult);
if not FileExists(Testname)
then ExcelDatenbank_anlegen
else ExcelDatenbank_oeffnen;
end;
end.