was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

56 Visitors Online


 
...ein TDataSet in MS Excel anzeigen?
Autor: JodeQa
[ Tip ausdrucken ]  

Tip Bewertung (11):  
     


{....}

uses DB;

{....}

  
private
    procedure 
SendToExcel(aDataSet: TDataSet);

{....}


uses
  
ComObj, ActiveX, Excel2000; // or Excel97

procedure TForm1.SendToExcel(aDataSet: TDataSet);
var
  
PreviewToExcel: TExcelApplication;
  RangeE: Excel2000.Range; //or RangeE: Excel97.Range
  
I, Row: Integer;
  Bookmark: TBookmarkStr;
begin
  
PreviewToExcel := TExcelApplication.Create(Self);
  PreviewToExcel.Connect;
  PreviewToExcel.Workbooks.Add(NULL, 0);
  RangeE := PreviewToExcel.ActiveCell;
 
  for I := 0 to aDataSet.Fields.Count - 1 do
  begin
    
RangeE.Value := aDataSet.Fields[I].DisplayLabel;
    RangeE := RangeE.Next;
  end;

  aDataSet.DisableControls;
  try
    
Bookmark := aDataSet.Bookmark;
    try
      
aDataSet.First;
      Row := 2;
      while not aDataSet.EOF do
      begin
        
//Write down Record As Row in msExcel
        
RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)];
        for I := 0 to aDataSet.Fields.Count - 1 do
        begin
          
RangeE.Value := aDataSet.Fields[I].AsString;
          RangeE := RangeE.Next;
        end;
        aDataSet.Next;
        Inc(Row);
      end;
    finally
      
aDataSet.Bookmark := Bookmark;
    end;
  finally
    
aDataSet.EnableControls;
  end;

  //Creating Preview from Range A1..ColumnX
  //Calculating ASCII 64 (Character Before "A") With Dataset FieldsCount
  //This Method can only handle range A1..Z?, if want to be excel column type
  //support, exp "AA"/"IV"
  
RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + IntToStr(Row - 1)];

  RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL);
  PreviewToExcel.Visible[0] := True;
  PreviewToExcel.Disconnect;
end;


// Beispiel:
// Example:
procedure TForm1.Button1Click(Sender: TObject);
begin
  
SendToExcel(Table1);
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners