Thema: Delphi Problem mit Excelexport

Einzelnen Beitrag anzeigen

Borschti

Registriert seit: 1. Nov 2007
Ort: Marburg Lahn
235 Beiträge
 
Delphi 2007 Professional
 
#9

Re: Problem mit Excelexport

  Alt 21. Jul 2008, 10:18
Hmm also ich habe auch nochmal ein bisschen rumprobiert und habe das jetzt so:

Delphi-Quellcode:
unit u_ExportEXCEL;

interface
uses classes;

type TXLSExport = class(TObject)
                  private
                     fs : TFilestream;
                     CXlsBof : array[0..5] of Word;
                     CXlsEof : array[0..1] of Word;
                     CXlsLabel : array[0..5] of Word;
                     CXlsNumber : array[0..4] of Word;
                     CXlsRk : array[0..4] of Word;
                  public
                     constructor Create(filename : string);
                     destructor Destroy; override;
                     procedure Write(const Col, Row: Word; const Value: Integer); overload;
                     procedure Write(const Col, Row: Word; const Value: Double); overload;
                     procedure Write(const Col, Row: Word; const Value: string); overload;
                  end;


implementation

constructor TXLSExport.Create(filename : string);
begin
  inherited Create;
  fs := TFileStream.Create(filename,fmCreate);
  fs.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;

destructor TXLSExport.Destroy;
begin
  fs.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
  inherited Destroy;
end;

procedure TXLSExport.Write(const Col, Row: Word; const Value: Integer);
var
  V: Integer;
begin
  CXlsRk[2] := Row;
  CXlsRk[3] := Col;
  fs.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
  V := (Value shl 2) or 2;
  fs.WriteBuffer(V, 4);
end;

procedure TXLSExport.Write(const Col, Row: Word; const Value: Double);
begin
  CXlsNumber[2] := Row;
  CXlsNumber[3] := Col;
  fs.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
  fs.WriteBuffer(Value, 8);
end;

procedure TXLSExport.Write(const Col, Row: Word; const Value: string);
var L: Word;
begin
  L := Length(Value);
  CXlsLabel[1] := 8 + L;
  CXlsLabel[2] := Row;
  CXlsLabel[3] := Col;
  CXlsLabel[5] := L;
  fs.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
  fs.WriteBuffer(Pointer(Value)^, L);
end;

end.
Mein Aufruf sieht so aus:

Delphi-Quellcode:
procedure TForm1.ExportierenExcel1Click(Sender: TObject);
var
  xf: TXLSExport;
  iCol, iRow: integer;
  Fields : TstringList;
  TempVal : String;
begin
  VddQuery1.First;

  if not SaveDialog1.Execute then Exit;

  if FileExists(Savedialog1.Filename) then
    DeleteFile(SaveDialog1.Filename);

  xf := TXLSExport.Create(savedialog1.filename);

  Fields := TStringList.Create;
  VddQuery1.GetFieldNames(Fields);

  for iRow := 0 to VddQuery1.RecordCount - 1 do
    begin
      for iCol := 0 to Fields.Count - 1 do
        begin
          TempVal := VddQuery1.FieldByName(Fields[iCol]).AsString;
          xf.Write(iCol, iRow, TempVal);
        end;
      VddQuery1.Next;
    end;

  xf.Free;
  Fields.Free;
end;
Also es werden auch schon sachen in die Exceldatei geschrieben aber leider vollkommen unleserlicher kram wie z.B. 500 etc.

Mir wird beim öffnen der .xls auch angezeigt das das Format der Datei nicht erkannt werden konnte

Woran liegt das denn?

mfg
Alex
  Mit Zitat antworten Zitat