unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB25_TLB, Excel2000, OleServer, StdCtrls, ExcelXP;
type
TFormQuery =
class(TForm)
ExcelApplication: TExcelApplication;
ExcelWorksheet: TExcelWorksheet;
ExcelWorkbook: TExcelWorkbook;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FLCID : Integer;
FConnectionString :
String;
procedure CloseExcel;
procedure OpenExcel;
public
{ Public-Deklarationen }
end;
var
FormQuery: TFormQuery;
implementation
{$R *.dfm}
resourcestring
ConnectionString = '
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sTest.mdb;Persist Security Info=False';
{ TFormQuery }
procedure TFormQuery.FormCreate(Sender: TObject);
begin
FLCID:=GetUserDefaultLCID;
FConnectionString:=Format(ConnectionString,[ExtractFilePath(ParamStr(0))]);
end;
procedure TFormQuery.OpenExcel;
begin
ExcelApplication.Connect;
// Stelle Verbindung zu Excel her
ExcelApplication.Visible[FLCID] := False;
// Starte Excel unsichtbar
ExcelApplication.UserControl:= False;
// UserControl muss immer syschron zu Visible gesetzt werden
ExcelApplication.DisplayAlerts[FLCID]:=False;
// Unterdrücke alle Fehlermeldungen von Excel
end;
procedure TFormQuery.CloseExcel;
begin
ExcelWorksheet.Disconnect;
ExcelWorkbook.Disconnect;
ExcelApplication.Quit;
ExcelApplication.Disconnect;
end;
procedure TFormQuery.Button1Click(Sender: TObject);
{
Achtung: Das Array wird über ArrV[Row,Col] (also ArrV[Y,X]) angesprochen,
weil Excel in den Zellbezüge auch ...[Row,Col] erwartet !!!!!!!!!!
}
var
Con : Connection;
// natives ADO-ConnectionObjekt
Rs : RecordSet;
// natives ADO-RecordsetObjekt
ArrV : Variant;
// Dies ist das variante Array
Col : Integer;
Row : Integer;
Cell : Range;
RecordCount : Integer;
// Anzahl der Datensätze
FieldsCount : Integer;
// Anzahl der Spalten (Felder)
begin
Screen.Cursor:=crHourGlass;
Col:=0;
Row:=0;
Con:=CoConnection.Create;
RS:=CoRecordSet.Create;
Try
RS.CursorLocation:=adUseClient;
// nur mit adUseClient ist die RecordCount Property gültig
Con.Open(FConnectionString,'
','
',-1);
// Öffne die Verbindung zur Datenbank
RS.Open('
SELECT * FROM daten',Con,adOpenForwardOnly,adLockReadOnly,adCmdTableDirect);
// Initialisiere ein zweidimensionales Array mit [Anzahl der Datensätze,Anzahl der Spalten]
RecordCount:=RS.RecordCount;
FieldsCount:=RS.Fields.Count;
ArrV:=VarArrayCreate([0,RecordCount-1,0,FieldsCount-1],varVariant);
VarArrayLock(ArrV);
While Not RS.EOF
do
begin
For Col:=0
to FieldsCount-1
do
ArrV[Row,Col]:=RS.Fields.Item[Col].Value;
Inc(Row);
RS.MoveNext;
end;
// While Not RS.EOF do
VarArrayUnLock(ArrV);
OpenExcel;
// Öffne jetzt Excel
Try
ExcelApplication.Workbooks.Open(ExtractFilePath(ParamStr(0))+'
Test.xls',EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,FLCID);
ExcelWorkbook.ConnectTo(ExcelApplication.Workbooks.Item['
Test.xls']);
ExcelWorkSheet.ConnectTo(ExcelWorkbook.Sheets.Item['
Tabelle1']
as _WorkSheet);
Cell:=ExcelWorkSheet.Cells.Range_['
B2','
B2'];
// legt die linke obere Ecke der Tabelle fest
ExcelWorkSheet.Range[Cell,Cell.Offset[RecordCount-1,FieldsCount-1]].Value:=ArrV;
// Übertrage Daten nach Excel
ExcelWorkbook.Save;
Finally
CloseExcel;
end;
Finally
RS:=Nil;
Con.Close;
Con:=Nil;
Screen.Cursor:=crDefault;
end;
end;
end.