Fórum Exportar dados de um DBGrid para o Excel ou Word #182760
18/09/2003
0
Ola pessoal,
Será que alguem pode me ajudar a exportar dados de um DBgrid para o excel ou pode ser ate mesmo no word, onde seja mais facil.
Agradeço muito a atenção !
Um abraço a todos
Diego
Será que alguem pode me ajudar a exportar dados de um DBgrid para o excel ou pode ser ate mesmo no word, onde seja mais facil.
Agradeço muito a atenção !
Um abraço a todos
Diego
Diego
Curtir tópico
+ 0
Responder
Posts
18/09/2003
Tio Frank
CARA EU TENHO ISSO AQUI MAS NUNCA USEI .
unit GridToExcel;
interface
Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, ExtCtrls, StdCtrls, ComCtrls, Db, DBTables, DBCtrls,
Buttons, ComObj, Filectrl;
function GridToExcelFile(Grid: TDbGrid;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 GridToExcelFile(Grid: TDbGrid;ExcelFile: String):Boolean;
var bResult: Boolean;
SavePlace: TBookmark;
i,eline: Integer;
Excel: Variant;
begin
bResult:= False;
// If dataset is assigned and active runs Excel
if Assigned(Grid.DataSource) then
begin
if Grid.DataSource.DataSet.Active then
begin
try
Excel:= CreateOleObject(´Excel.Application´);
Excel.Visible:= False;
Excel.WorkBooks.Add;
// Save grid Position
SavePlace:= Grid.DataSource.DataSet.GetBookmark;
Grid.DataSource.DataSet.First;
// Dataset Header
if not (Grid.DataSource.DataSet.Eof) then
begin
eline:= 1; // First Line of Sheet
for i:=0 to (Grid.Columns.Count-1) do
begin
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)] := Grid.Columns[i].Title.Caption;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].ColumnWidth := Grid.Columns[i].Field.DisplayWidth;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.FontStyle := ´Negrito´;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Interior.Color := (ColorToRgb(Grid.Columns[i].Title.Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.Color := (ColorToRgb(Grid.Columns[i].Title.Font.Color));
end;
end;
while not Grid.DataSource.DataSet.Eof do // Detail
begin
Inc(eline); // Add 1 to the line of Sheet
for i:=0 to (Grid.Columns.Count-1) do
begin
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)] := Grid.Columns[i].Field.AsString;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Interior.Color := (ColorToRgb(Grid.Columns[i].Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.Color := (ColorToRgb(Grid.Columns[i].Font.Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Borders.Color := (ColorToRgb(Grid.FixedColor));
end;
Grid.DataSource.DataSet.Next;
end;
// Set saved grid position
Grid.DataSource.DataSet.GotoBookmark(SavePlace);
// Save the file
Excel.WorkBooks[1].SaveAs(ExcelFile);
Excel.Quit;
bResult:= True;
except
bResult:= False;
end;
end;
end;
Result := bResult;
end;
QUALQUER COISA MANDA AÍ.
unit GridToExcel;
interface
Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, ExtCtrls, StdCtrls, ComCtrls, Db, DBTables, DBCtrls,
Buttons, ComObj, Filectrl;
function GridToExcelFile(Grid: TDbGrid;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 GridToExcelFile(Grid: TDbGrid;ExcelFile: String):Boolean;
var bResult: Boolean;
SavePlace: TBookmark;
i,eline: Integer;
Excel: Variant;
begin
bResult:= False;
// If dataset is assigned and active runs Excel
if Assigned(Grid.DataSource) then
begin
if Grid.DataSource.DataSet.Active then
begin
try
Excel:= CreateOleObject(´Excel.Application´);
Excel.Visible:= False;
Excel.WorkBooks.Add;
// Save grid Position
SavePlace:= Grid.DataSource.DataSet.GetBookmark;
Grid.DataSource.DataSet.First;
// Dataset Header
if not (Grid.DataSource.DataSet.Eof) then
begin
eline:= 1; // First Line of Sheet
for i:=0 to (Grid.Columns.Count-1) do
begin
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)] := Grid.Columns[i].Title.Caption;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].ColumnWidth := Grid.Columns[i].Field.DisplayWidth;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.FontStyle := ´Negrito´;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Interior.Color := (ColorToRgb(Grid.Columns[i].Title.Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.Color := (ColorToRgb(Grid.Columns[i].Title.Font.Color));
end;
end;
while not Grid.DataSource.DataSet.Eof do // Detail
begin
Inc(eline); // Add 1 to the line of Sheet
for i:=0 to (Grid.Columns.Count-1) do
begin
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)] := Grid.Columns[i].Field.AsString;
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Interior.Color := (ColorToRgb(Grid.Columns[i].Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Font.Color := (ColorToRgb(Grid.Columns[i].Font.Color));
Excel.WorkBooks[1].Sheets[1].Cells[eline,(i+1)].Borders.Color := (ColorToRgb(Grid.FixedColor));
end;
Grid.DataSource.DataSet.Next;
end;
// Set saved grid position
Grid.DataSource.DataSet.GotoBookmark(SavePlace);
// Save the file
Excel.WorkBooks[1].SaveAs(ExcelFile);
Excel.Quit;
bResult:= True;
except
bResult:= False;
end;
end;
end;
Result := bResult;
end;
QUALQUER COISA MANDA AÍ.
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)