1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, StdCtrls, ADODB, ComCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ProgressBar1: TProgressBar;
Button2: TButton;
q1: TADOQuery;
OpenDialog1: TOpenDialog;
q1ID: TAutoIncField;
q1Name: TWideStringField;
q1Adress: TWideStringField;
q1Dept: TWideStringField;
q1Champ1: TWideStringField;
Button4: TButton;
Button3: TButton;
procedure ExportTableToExcel(Table:TadoTable;sFile:string);
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
USES ComObj,ExcelXP,strUtils;
{$R *.dfm}
procedure TForm1.ExportTableToExcel(Table:TadoTable;sFile:string); // db to Excel
var
//Declarations
ExcelApplication : variant;
Sheet : variant;
row ,rowsno: integer;
begin
Cursor:=crHourGlass;
try //Try to create Excel application
begin
ExcelApplication := CreateOleObject('Excel.Application');
ExcelApplication.Visible := true; //let's make visible
end;
except
//If failed then show warning
Showmessage('Cannot create an Excel file,'
+'make sure that MS Excel is installed on your system');
Application.Terminate;
end;
rowsno:=Table.RecordCount; //records number in a table
Table.RecNo:=1; //set table to first record
ExcelApplication.WorkBooks.Add(-4167); //Add excel workbook
ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'my data';
Sheet := ExcelApplication.WorkBooks[1].WorkSheets['my data'];
//Format cells in excel sheet
Sheet.Range['A1:G'+IntToStr(rowsno+1)].Borders.LineStyle := 7;
Sheet.Range['A1:G'+IntToStr(rowsno+1)].Borders.color := clblue;
Sheet.Range['B2:B'+IntToStr(rowsno+1)].HorizontalAlignment :=xlLeft;
//colors of cells in first line
Sheet.Cells[1,1].Interior.Color := clMoneyGreen;
Sheet.Cells[1,2].Interior.Color := clMoneyGreen;
Sheet.Cells[1,3].Interior.Color := clMoneyGreen;
Sheet.Cells[1,4].Interior.Color := clMoneyGreen;
Sheet.Cells[1,5].Interior.Color := clMoneyGreen;
Sheet.Cells[1,6].Interior.Color := clMoneyGreen;
Sheet.Cells[1,7].Interior.Color := clMoneyGreen;
//widths of columns
Sheet.Columns[1].ColumnWidth := 10;
Sheet.Columns[2].ColumnWidth := 10;
Sheet.Columns[3].ColumnWidth := 10;
//captions/text of cells
Sheet.Cells[1,1] := 'N°';
Sheet.Cells[1,2] := 'Feuilles';
Sheet.Cells[1,3] := 'Verte';
Sheet.Cells[1,4] := 'Jaune';
Sheet.Cells[1,5] := 'Orange';
Sheet.Cells[1,6] := 'Rouge';
Sheet.Cells[1,7] := 'Feuille';
//set progressbar max value = teble records count
ProgressBar1.max:=Table.RecordCount;
ProgressBar1.position:=0; //set position to 0=start
//now copy from table to excel cells
for row := 1 to Table.RecordCount do
begin
Sheet.Cells[row+1,1] := Table.Fields[0].AsString; //row+1 = move to the next line
//in column one
Sheet.Cells[row+1,2] :=Table.Fields[1].AsString;
//row+1 = move to the next line
//in column two
Sheet.Cells[row+1,3] := Table.Fields[2].AsFloat;
Sheet.Cells[row+1,4] := Table.Fields[3].AsFloat;
Sheet.Cells[row+1,5] := Table.Fields[4].AsFloat;
Sheet.Cells[row+1,6] := Table.Fields[5].AsFloat;
Sheet.Cells[row+1,7] := Table.Fields[6].AsFloat;
//row+1 = move to the next line
//in column three
ProgressBar1.position:=row;//set progress bar position
Table.Next; //go to the next record in a table
end;
Screen.Cursor:=crDefault; //set default cursor
ProgressBar1.position:=0; //it is done , set PB to the begining
DeleteFile(sFile); //
Sheet.SaveAs(sFile); //Save excel file
{
ExcelApplication.Quit; //Quit excel
ExcelApplication := Unassigned; //to be sure that no hidden excel in memory
Sheet := Unassigned;
}
//let's close manually
Screen.Cursor:=crDefault; //set default cursor
//let's test
//a bit slow while screen capturing
end;
// ********** pour l'appeler
procedure TForm1.Button1Click(Sender: TObject);
begin
ExportTableToExcel(adotable1,'c:\temp\mmm.xlsx'); // db to Excel
end; |