gerar .xls

Delphi

16/09/2005

Olá, tenho um sql no delphi que preciso que o resultado seja transfomado num arquivo do excel.
Alguém sabe como fazer?


Daia

Daia

Curtidas 0

Respostas

Motta

Motta

16/09/2005

Pergunta frequente, dê uma pesquisada e vai ver varias soluções para este problema.


GOSTEI 0
Daia

Daia

16/09/2005

Obrigada,
Pesquisei e achei parte da resposta...
Baixei um componente: mxNativeExcel... mas não faço a mínima idéia de como trabalhar com ele, será q pode me ajudar??


GOSTEI 0
Motta

Motta

16/09/2005

use esta function :

unit DataSetToExcel; 

interface 

Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  Grids, DBGrids, ExtCtrls, StdCtrls, ComCtrls, Db, DBTables, DBCtrls, 
  Buttons, ComObj, Filectrl,OleServer, Excel97; 

  function DataSetToExcelFile(ds: TDataSet;ExcelFile: String):Boolean; 

implementation 

// Generate a Excel File (.xls) with the DataSource.DataSet 
// property of the grid. 
// Format the TFields of the DataSet to show in the Excel. 
function DataSetToExcelFile(ds: TDataSet;ExcelFile: String):Boolean; 
var bResult: Boolean; 
    SavePlace: TBookmark; 
    i,eline: Integer; 
    Excel: TExcelApplication; 
    r : Excel97.Range; 
    w : _WorkBook; 
    celula : string; 
begin 
  bResult:= False; 
  // If dataset is assigned and active runs Excel 
  if Assigned(ds) then 
  begin 
    if ds.Active then 
    begin 
      ds.DisableControls; 
      try 
        Excel:= TExcelApplication.Create(Application); 
        Excel.Connect; 
        Excel.Visible[0] := False; 
        w := Excel.WorkBooks.Add(null,0); 
        r := Excel.ActiveCell; 
        // Save grid Position 
        SavePlace:= ds.GetBookmark; 
        ds.First; 
        // Dataset Header 
        eline:= 1; // First Line of Sheet 
        if not (ds.Eof) then 
        begin 
          for i:=0 to (ds.FieldCount-1) do 
          begin 
            r.Value := ds.Fields[i].DisplayLabel; 
            r := r.Next; 
          end; 
        end; 
        while not ds.Eof do // Detail 
        begin 
          Inc(eline); // Add 1 to the line of Sheet 
          celula := ´A´ + IntToStr(eline); 
          r := Excel.Range[celula,celula]; 
          for i:=0 to (ds.FieldCount-1) do 
          begin 
            r.Value := ds.Fields[i].Value; 
            r := r.Next; 
          end; 
          ds.Next; 
        end; 
        // Set saved grid position 
        ds.GotoBookmark(SavePlace); 
        // Save the file 
        w.SaveAs(ExcelFile,null,null,null,null,null,ACCESS_OBJECT_GUID,null,null,null,null,0); 
        w.Close(null,null,null,0); 
        Excel.Disconnect; 
        Excel.Quit; 
        Excel.Free; 
        bResult:= True; 
      except 
        on e:exception do 
        begin 
          showmessage(e.message); 
          ds.EnableControls; 
          bResult:= False; 
        end; 
      end; 
    end; 
  end; 
  ds.EnableControls; 
  Result := bResult; 
end; 

end. 





passe um dataset (query) e o nome do arquivo a ser gerado, requer porém que a máquina tenha excel instalado.

não conheço o comp. mxNativeExcel


GOSTEI 0
POSTAR