Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Listview Inhalt als *.xls für Excel abspeichern (https://www.delphipraxis.net/125334-listview-inhalt-als-%2A-xls-fuer-excel-abspeichern.html)

Ajin 4. Dez 2008 10:42


Listview Inhalt als *.xls für Excel abspeichern
 
Hallo DP,

Ich betrete mal wieder Neuland, diesmal möchte ich den Inhalt einer Listview als *.xls abspeichern.
Ich nutze Codegear Delphi for Microsoft Windows 2007.

Folgende Komponenten befinden sich auf meinem Formular:

ExcelApp: TExcelApplication
ExcelOLEObject1: TExcelOLEObject
Erfassungsdaten: TExcelWorksheet
ExcelWorkbook1: TExcelWorkbook

Auto Connect ist FALSE
und ConnectKind ist: ckRunningOrNew

Bevor ich mich an die Listview wage, wollte ich zunächst mal ganz simpel ein Wort in Zeile1, Spalte1 einfügen. Das ist der Code dafür:

Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var
flcid:integer;
Filename:String;
begin
 SaveDialog.Execute;

    case SaveDialog.FilterIndex of
      1: Filename := ChangeFileExt(SaveDialog.FileName,'.xls');
      2: Filename := ChangeFileExt(SaveDialog.FileName,'.xls');
    end;

    if Filename <> '' then
      begin
        try
          flcid:=GetUserDefaultLCID;
          ExcelApp.Connect;
          ExcelApp.Visible[flcid]:=true;
          ExcelApp.UserControl:=true;
        Except
          //fehler ausgeben
        end;

        CreateFile(@Filename[1], 0, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

        ExcelWorkbook1.ConnectTo(ExcelApp.Workbooks.Open(filename, False,
        False, EmptyParam, '', False, False, EmptyParam, EmptyParam, false, false,
        EmptyParam, EmptyParam, EmptyParam, false, 0));
        Erfassungsdaten.ConnectTo(ExcelWorkbook1.Sheets.Item[1] as ExcelWorkSheet);

        ExcelApp.Cells[1, 1] := 'TESTEINTRAG';

      end;

   //Aufräumen
   ExcelApp.Quit;
end;
Das funktioniert nicht ganz denn es erscheint ein Fehler: [DCC Fehler] Unit1.pas(220): E2149 Klasse besitzt keine Standardeigenschaft

Bei der Zeile: ExcelApp.Cells[1, 1] := 'TESTEINTRAG';

Gemäß Suchfunktion wird aber genau diese Zeile für das Eintragen in einem Excelsheet genutut. Wo liegt denn da der Fehler?

nahpets 4. Dez 2008 10:47

Re: Listview Inhalt als *.xls für Excel abspeichern
 
Hallo,

runde Klammern? ist kein Array wie bei StringGrids.

Relicted 4. Dez 2008 11:55

Re: Listview Inhalt als *.xls für Excel abspeichern
 
Moin,

ich gehe davon aus, dass in die entsprechende Zeile noch etwas ähnliches wie .Text oder so gehangen werden muss. Daher auch der Fehler mit der "gibt keine Standardeigenschaft".
Eine andere Idee wäre es das ganze einfach als CSV zu speichern. Sobald man Excel installiert hat ist Excel auch das Standardprogramm für *.csv Dateien.

Gruß
Reli

XiaN 4. Dez 2008 13:13

Re: Listview Inhalt als *.xls für Excel abspeichern
 
Hier mal ein nativer Weg .xls zu erstellen. Dafür brauchste dann kein Ole, quasi kein Excel aufm Rechner.

CSV und Excel ist übrigens so ne Sache. M$ hat von Excel 6 zu 2003 von sich aus den CSV Standard geändert.
Wenn ich mich recht entsinne, dann wurde früher ein CSV so getrennt : "Eintrag","Eintrag","Eintrag" und jetz isses Eintrag;Eintrag;Eintrag ( oder umgekehrt .. Ich weiß es wirklich nich mehr ^^ )

Ausserdem verhunzt dir Excel beim öffnen von CSV die darin enthaltenen Daten. Z.B. werden bei allen Feldern die mit 0 beginnen ( Telefonnr. z.B. ) bei Excel dann keine führenden Nullen mehr angezeigt.

Mit dem Source unten geht der Export zu Excel ohne Probs.

Is hier zwar ein TZQuery, aber ich denke das lässt sich ohne Probleme abändern.

Delphi-Quellcode:
function TReport.QueryToXLS(Query: TZQuery; FilePath: string): boolean;

  procedure WriteXLSCell(XLS: TStream; const Row, Col: Word; const Value: string);
  var
    L: Word;
    XLSLabel: array[0..5] of Word;
  begin
    L := Length(Value);
    XLSLabel[0] := $204;
    XLSLabel[1] := 8 + L;
    XLSLabel[2] := Row;
    XLSLabel[3] := Col;
    XLSLabel[4] := 0;
    XLSLabel[5] := L;
    XLS.WriteBuffer(XLSLabel, SizeOf(XLSLabel));
    XLS.WriteBuffer(Pointer(Value)^, L);
  end;

const
  XLSBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0);
  XLSEof: array[0..1] of Word = ($0A, 00);

var
  XLS: TFileStream;
  Row, Col: Integer;
  Columns: TStrings;

begin

  XLS := TFileStream.Create(FilePath, fmCreate);
  Columns := TSTringList.Create;
  Row := 0;

  Result := true;

  try

    // First we write the "Beginning of File"

    XLS.WriteBuffer(XLSBof, SizeOf(XLSBof));

    // Then we write the columns

    Query.GetFieldNames(Columns);

    for Col := 0 to Columns.Count - 1 do
    begin
      WriteXLSCell(XLS, Row, Col, GetHeader(Columns[col]));
    end;

    // Then the actual content

    while not Query.Eof do
    begin

      Row := Row + 1;

      for Col := 0 to Columns.Count - 1 do
      begin
        WriteXLSCell(XLS, Row, Col, Query.FieldByName(Columns[col]).AsString);
      end;

      Query.Next;

    end;

    // And last but not least the "End of File"

    XLS.WriteBuffer(XLSEof, SizeOf(XLSEof));

  finally

    XLS.Free;
    Columns.Free;

  end;

end;

function TReport.QueryToCSV(Query: TZQuery): TStringList;
var
  Columns: TStrings;
  Row: string;
  i: integer;

begin

  Columns := TStringList.Create;
  Result := TStringList.Create;
  Row := '';

  Query.GetFieldNames(Columns);

  for i := 0 to Query.FieldCount - 1 do
  begin
    Row := Row + GetHeader(Columns.Strings[i]) + #9;
  end;

  SetLength(Row, Length(Row) - 1);

  Result.Add(Row);

  while not Query.Eof do
  begin

    Row := '';

    for i := 0 to Columns.Count - 1 do
    begin
      Row := Row + AnsiReplaceStr(AnsiReplaceStr(Query.FieldByName(Columns[i]).AsString, #13#10, ' '), #9, ' ') + #9;
    end;

    SetLength(Row, Length(Row) - 1);

    Result.Add(Row);

    Query.Next;

  end;

  FreeAndNil(Columns);

end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:09 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz