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: TExcelApplication;
FExcelWorkbook: TExcelWorkbook;
FExcelWorksheet: TExcelWorksheet;
implementation
{$R *.dfm}
procedure ExcelDatenbank_anlegen;
var sparentroot:
string;
iUserLCID: integer;
begin
sparentroot := ExtractFilePath(ParamStr(0));
iUserLCID := GetUserDefaultLCID;
FExcelApplication := TExcelApplication.Create(
Nil);
FExcelApplication.Workbooks.Add(emptyparam, iUserLCID);
FExcelWorkbook := TExcelWorkbook.create(
Nil);
FExcelWorkbook.ConnectTo(FExcelApplication.ActiveWorkbook
as ExcelWorkbook);
FExcelWorkbook.Worksheets.Add(emptyParam, emptyParam, emptyParam, emptyParam, iUserLCID);
FExcelWorksheet := TExcelWorksheet.create(
Nil);
FExcelWorksheet.ConnectTo(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';
FExcelWorkbook.Close(true, sparentroot + Testname, emptyParam, iUserLCID);
// Speichert die Änderungen in Excel } //Speicherleck
FExcelWorksheet.Free;
FExcelWorkbook.Free;
FExcelApplication.Free;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if not (csDesigning
in Self.ComponentState)
then
begin
FExcelWorkbook.Close(true);
// Speicherleck
FExcelWorksheet.Free;
FExcelWorkbook.Free;
FExcelApplication.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var iUserLCID: integer;
DWResult: DWORD;
begin
if not FileExists(Testname)
then ExcelDatenbank_anlegen;
iUserLCID := GetUserDefaultLCID;
// Wenn die Datenbank extern über Excel geöffnet ist dann wird sie geschlossen.
// wenn diese Datei nicht geschlossen wird, dann kommt es zu einer Exception!!!
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);
FExcelApplication := TExcelApplication.Create(
Nil);
FExcelWorkbook := TExcelWorkbook.create(
Nil);
FExcelWorksheet := TExcelWorksheet.create(
Nil);
// vorhandener Exceldatei laden !!! wichtig !!! Die Anzahl der emtyParam ist Office-Versions abhängig
FExcelApplication.Workbooks.Open(ExtractFilePath(ParamStr(0)) + TestName, emptyParam, emptyParam, emptyParam, emptyParam
, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam
, emptyParam, emptyParam, iUserLCID);
// Excel soll NICHT angezeigt werden
FExcelApplication.visible[iUserLCID] := true;
// verbinden des Workbooks und des Worksheets mit der in der exc geladenen Datei
FExcelWorkbook.ConnectTo(FExcelApplication.ActiveWorkbook);
FExcelWorksheet.ConnectTo(FExcelWorkbook.ActiveSheet
as ExcelWorksheet);
end;
end.