Announcement

Collapse
No announcement yet.

Export nach Excel aus TStringList

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export nach Excel aus TStringList

    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

  • #2
    Hallo Uwe,<br>
    versuche es mal mit cSheet = 'Labels$'; <br>
    machmal hilft es bei Excel, wenn man ein $ Zeichen an den Worksheetnamen hängt

    Comment


    • #3
      Hallo Jens,

      diese Excel Datei soll vor dem Export neu erstellt und gespeichert werden (mit TSaveDialog), eines der drei WorkSheets 'Labels' benannt und dann die StringList in die Dateiexportiert werden.

      Any idea?

      Gruss
      Uw

      Comment


      • #4
        Hallo Jens,

        nach langem Stöbern im Forum (hauptsächl in COM+/DCOM) bin ich jetzt auf die Lösung gekommen:
        <PRE>
        begin
        with SaveDialog1 do begin
        DefaultExt:= '*.xls';
        Filter:= 'MS Excel - Datei|*.xls';
        end;
        if SaveDialog1.Execute then
        begin
        vExcel:= UnAssigned;
        sDBPath := SaveDialog1.FileName;
        if FileExists(sDBPath) then
        DeleteFile(sDBPath);
        try
        vExcel := CreateOleObject('Excel.Application');
        except
        ShowMessage('Fehler');
        LabelListe.Free;
        Exit;
        end;
        vWorkbook := vExcel.WorkBooks.Add;
        for ICnt:= 0 to LabelListe.Count - 1 do
        vExcel.Cells[iCnt + 1, 1].Value := LabelListe[iCnt];
        vWorkBook.SaveAs(sDBPath, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, False, EmptyParam, EmptyParam, GetUserDefaultLCID);
        vExcel.Workbooks.Close;
        vExcel.Quit;
        end;
        end;
        </PRE>

        Gruss
        Uw

        Comment

        Working...
        X