Einzelnen Beitrag anzeigen

omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#2

Re: Inhald eines DBGrid nach Excel exportieren ?

  Alt 17. Jun 2006, 17:31
Hallo Vader,

entweder du schreibst die Zeilen selber nach Excel...

Delphi-Quellcode:
uses ... OleServer, Excel2000 ...
:
:
var Excel:TExcelApplication;
    lcid:integer;
    Workbook:_Workbook;
    Inhalt:Variant;
begin
  Excel:=TExcelApplication.Create(Self);
  try
    lcid:=GetUserDefaultLCID;
    Excel.Connect;
    Excel.Visible[lcid] := true;
    Excel.UserControl:=true;

    Workbook:=Excel.Workbooks.Add(EmptyParam, lcid);

    Inhalt:='Hallo';
    Excel.Cells.Range['A1', 'A1'].Value:=Inhalt;

  finally
    Excel.Disconnect;
    Excel.free;
  end;
end;
Oder du übergibst die SQL-Abfrage, die dein DBGrid anzeigt an Excel...
(Excel holt dann selbst die Daten von der Datenbank)

Delphi-Quellcode:
uses ... OleServer, Excel2000 ...
:
:
var Excel:TExcelApplication;
    lcid:integer;
    Workbook:_Workbook;
    Query, Range, Sheet:Variant;
    User, Database, ODBCname:string;
begin
  Excel:=TExcelApplication.Create(Self);
  try
    lcid:=GetUserDefaultLCID;
    Excel.Connect;
    Excel.Visible[lcid] := true;
    Excel.UserControl := true;

    Workbook:=Excel.Workbooks.Add(EmptyParam, LCID);

    User:='ich';
    ODBCname:='LocalServer';
    Database:='MeineDatenbank';

    Sheet:=Workbook.ActiveSheet;
    Range := Excel.Cells.Range['A1', 'A1'];
    Query:=Sheet.QueryTables.Add(
      'ODBC;' +
      'DSN=' + ODBCname + ';' +
      'UID=' + User + ';' +
      'APP=Microsoft® Query;' +
      'DATABASE=' + Database + ';' +
      'Trusted_Connection=Yes',
      Range
    );
    Query.CommandText:='SELECT * FROM tabelle';
    Query.Name:= 'Meine Abfrage von LocalServer';
    Query.FieldNames := True;
    Query.RowNumbers := False;
    Query.FillAdjacentFormulas := False;
    Query.PreserveFormatting := True;
    Query.RefreshOnFileOpen := False;
    Query.BackgroundQuery := True;
    Query.RefreshStyle := xlInsertDeleteCells;
    Query.SavePassword := True;
    Query.SaveData := True;
    Query.AdjustColumnWidth := True;
    Query.RefreshPeriod := 0;
    Query.PreserveColumnInfo := True;
    Query.Refresh(False);
  finally
    Excel.Disconnect;
    Excel.free;
  end;
end;
Dafür muss allerdings die MSQuery installiert sein.

Edit...
und hier nochmal eine Methode, die ein DBGrid erwartet und den Inhalt direkt nach Excel schreibt...

Delphi-Quellcode:
procedure DBGridToExcel(DBGrid:TDBGrid; StartSpalte, StartZeile:integer);
type TSpalten = array[1..256] of string;

  function CreateSpalten:TSpalten;
  var i, j, x:integer;
      abbruch:boolean;
  begin
    x:=1;
    abbruch:=false;
    j:=0;
    while (j <= 26) and not abbruch do begin
      i:=1;
      while (i <= 26) and not abbruch do begin
        if j = 0 then
          Result[x]:=chr(i+64)
        else
          Result[x]:=chr(j+64)+chr(i+64);
        inc(i);
        inc(x);
        abbruch:=(x > 256);
      end;
      inc(j);
    end;
  end;

var Excel:TExcelApplication;
    i, Zeile, lcid:integer;
    Workbook:_Workbook;
    Sheet, Zelle, Inhalt:Variant;
    Spalten:TSpalten;
begin
  if assigned(DBGrid)
     and assigned(DBGrid.DataSource)
     and assigned(DBGrid.DataSource.DataSet) then
  begin
    if DBGrid.DataSource.DataSet.Active then begin
      Excel:=TExcelApplication.Create(nil);
      try
        lcid:=GetUserDefaultLCID;
        Excel.Connect;
        Excel.Visible[lcid]:=true;
        Excel.UserControl:=true;

        Workbook:=Excel.Workbooks.Add(EmptyParam, lcid);

        Spalten:=CreateSpalten;
        Zeile:=StartZeile;
        for i:=1 to DBGrid.FieldCount do begin
          Inhalt:=DBGrid.Fields[i-1].DisplayName;
          Zelle:=Excel.Cells.Range[
            Spalten[i+StartSpalte-1]+inttostr(Zeile),
            Spalten[i+StartSpalte-1]+inttostr(Zeile)
          ];
          Zelle.Value:=Inhalt;
          Zelle.Font.Bold:=true;
        end;

        DBGrid.DataSource.DataSet.First;
        while not DBGrid.DataSource.DataSet.Eof do begin
          inc(Zeile);
          for i:=1 to DBGrid.FieldCount do begin
            Inhalt:=DBGrid.DataSource.DataSet.FieldByName(
              DBGrid.Fields[i-1].FieldName
            ).AsString;
            Zelle:=Excel.Cells.Range[
              Spalten[i+StartSpalte-1]+inttostr(Zeile),
              Spalten[i+StartSpalte-1]+inttostr(Zeile)
            ];
            Zelle.Value:=Inhalt;
          end;
          DBGrid.DataSource.DataSet.Next;
        end;

        Sheet:=Workbook.ActiveSheet;
        Sheet.Columns[
          Spalten[StartSpalte]+':'+Spalten[StartSpalte+DBGrid.FieldCount]
        ].EntireColumn.AutoFit;

      finally
        Excel.Disconnect;
        Excel.free;
      end;
    end;
  end;
end;
MfG
Thorsten
  Mit Zitat antworten Zitat