Hallo Gemeinde,
trotz Lektüre von Andreas's Bücher über COM und ADO komme ich leider nicht weiter.
Ich habe eine gefüllte Stringliste und möchte diese gerne nach Excel exportieren. Bei Access und Text-Datei ging's, aber Excel stellt mich vor unlösbare Probleme. Verwendet wird D4 mit nativen ADOs und Excel 2000. Nachfolgend der Source Code:
<PRE>
procedure TfmMain.ExportLabel;
resourcestring
cAccess = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=''%s''';
cExcel = 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;' +
'Extended Properties=Excel 8.0;' +
'Persist Security Info=False';
c1 = 'INSERT INTO Labels (Werte) VALUES (''%s'');';
var
aCon : Connection;
vRows : OleVariant;
iCnt : SmallInt;
SteuerString, sDBPath: String;
fs : TFileStream;
begin
case rgSelectExport.ItemIndex of //es gibt ein TRadioGroup
1:
begin
with SaveDialog1 do begin
DefaultExt:= '*.xls';
Filter:= 'MS Excel - Datei|*.xls';
end;
if SaveDialog1.Execute then
begin
sDBPath := SaveDialog1.FileName;
MakeExcel(sDBPath);
aCon:= CoConnection.Create;
aCon.cursorLocation:= adUseClient;
aCon.Open(Format(cExcel,[sDBPath]), '', '', adConnectUnspecified);
try
for iCnt:= 0 to LabelListe.Count - 1 do begin
aCon.Execute(Format(c1, [LabelListe[iCnt]]), vRows, adExecuteNoRecords);
end;
finally
aCon.close;
aCon:= nil;
end;
end;
end;
</PRE>
Aber bis dahin komme ich gar nicht, weil es hier schon Ärger gibt:
<PRE>
procedure TfmMain.MakeExcel(sDBPath: String);
resourcestring
cSheet = 'Labels';
var
iCnt : SmallInt;
aConJET : _Connection;
aRSJet : _Recordset;
cCSJET : String;
begin
// Verbindung zu Excel herstellen
cCSJET:= 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;' +
'Extended Properties=Excel 8.0;' +
'Persist Security Info=False';
cCSJET:= Format(cCSJET,[sDBPath]);
aConJET:= CoConnection.Create;
aConJET.CursorLocation := adUseClient;
aConJET.Open(cCSJET, '', '', adConnectUnspecified);
try
aRSJet := CoRecordSet.Create;
aRSJet.Set_ActiveConnection(aConJET);
aRSJet.Open(Labels, EmptyParam, adOpenStatic, // <-- er meckert es gäbe "Labels" nicht, obwohl es das Arbeitsblatt "Labels" gibt
adLockOptimistic, adCmdTableDirect);
//Daten in die XLS-Tabelle schreiben
for iCnt:= 0 to LabelListe.Count - 1 do
begin
aRSJet.AddNew(EmptyParam, EmptyParam);
aRSJet.Fields.Item[0].Value := LabelListe[iCnt];
aRSJet.Update(EmptyParam, EmptyParam);
end;
aRSJet.Close;
aRSJet := nil;
finally
aConJET.Close;
aConJET := nil;
end;
end;
</PRE>
Wer weiss, wie ich da raus komme?
Viele Grüsse
Uwe
trotz Lektüre von Andreas's Bücher über COM und ADO komme ich leider nicht weiter.
Ich habe eine gefüllte Stringliste und möchte diese gerne nach Excel exportieren. Bei Access und Text-Datei ging's, aber Excel stellt mich vor unlösbare Probleme. Verwendet wird D4 mit nativen ADOs und Excel 2000. Nachfolgend der Source Code:
<PRE>
procedure TfmMain.ExportLabel;
resourcestring
cAccess = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=''%s''';
cExcel = 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;' +
'Extended Properties=Excel 8.0;' +
'Persist Security Info=False';
c1 = 'INSERT INTO Labels (Werte) VALUES (''%s'');';
var
aCon : Connection;
vRows : OleVariant;
iCnt : SmallInt;
SteuerString, sDBPath: String;
fs : TFileStream;
begin
case rgSelectExport.ItemIndex of //es gibt ein TRadioGroup
1:
begin
with SaveDialog1 do begin
DefaultExt:= '*.xls';
Filter:= 'MS Excel - Datei|*.xls';
end;
if SaveDialog1.Execute then
begin
sDBPath := SaveDialog1.FileName;
MakeExcel(sDBPath);
aCon:= CoConnection.Create;
aCon.cursorLocation:= adUseClient;
aCon.Open(Format(cExcel,[sDBPath]), '', '', adConnectUnspecified);
try
for iCnt:= 0 to LabelListe.Count - 1 do begin
aCon.Execute(Format(c1, [LabelListe[iCnt]]), vRows, adExecuteNoRecords);
end;
finally
aCon.close;
aCon:= nil;
end;
end;
end;
</PRE>
Aber bis dahin komme ich gar nicht, weil es hier schon Ärger gibt:
<PRE>
procedure TfmMain.MakeExcel(sDBPath: String);
resourcestring
cSheet = 'Labels';
var
iCnt : SmallInt;
aConJET : _Connection;
aRSJet : _Recordset;
cCSJET : String;
begin
// Verbindung zu Excel herstellen
cCSJET:= 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;' +
'Extended Properties=Excel 8.0;' +
'Persist Security Info=False';
cCSJET:= Format(cCSJET,[sDBPath]);
aConJET:= CoConnection.Create;
aConJET.CursorLocation := adUseClient;
aConJET.Open(cCSJET, '', '', adConnectUnspecified);
try
aRSJet := CoRecordSet.Create;
aRSJet.Set_ActiveConnection(aConJET);
aRSJet.Open(Labels, EmptyParam, adOpenStatic, // <-- er meckert es gäbe "Labels" nicht, obwohl es das Arbeitsblatt "Labels" gibt
adLockOptimistic, adCmdTableDirect);
//Daten in die XLS-Tabelle schreiben
for iCnt:= 0 to LabelListe.Count - 1 do
begin
aRSJet.AddNew(EmptyParam, EmptyParam);
aRSJet.Fields.Item[0].Value := LabelListe[iCnt];
aRSJet.Update(EmptyParam, EmptyParam);
end;
aRSJet.Close;
aRSJet := nil;
finally
aConJET.Close;
aConJET := nil;
end;
end;
</PRE>
Wer weiss, wie ich da raus komme?
Viele Grüsse
Uwe
Comment