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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls ;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
ExcelApp: OleVariant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
// SheetType
xlChart = -4109;
xlWorksheet = -4167;
// WBATemplate
xlWBATWorksheet = -4167;
xlWBATChart = -4109;
// Page Setup
xlPortrait = 1;
xlLandscape = 2;
xlPaperA4 = 9;
// Format Cells
xlBottom = -4107;
xlLeft = -4131;
xlRight = -4152;
xlTop = -4160;
// Text Alignment
xlHAlignCenter = -4108;
xlVAlignCenter = -4108;
// Cell Borders
xlThick = 4;
xlThin = 2;
var
ColumnRange: OleVariant;
begin
try
ExcelApp := GetActiveOleObject('Excel.Application');
except
try
ExcelApp := CreateOleObject('Excel.Application');
except
ShowMessage('Impossible d''ouvrir Excel ?');
Exit;
end;
end;
// Ouverture du fichier Excel
ExcelApp.Workbooks.Open(ExtractFilePath(application.Exename)+'Test.xls');
// Renommage de la feuille 1
ExcelApp.Workbooks[1].WorkSheets[1].Name := 'Doc 1';
// Insertion de texte cellule par cellule
ExcelApp.Cells[1, 1].Value := 'Test Excel';
ExcelApp.Cells[2, 1].Value := 'Format de cellules';
ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);
// Série de valeurs
ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);
// Alignement
ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;
// Largeur colonnes
ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;
ColumnRange.Columns[1].ColumnWidth := 20;
ColumnRange.Columns[2].ColumnWidth := 40;
// Hauteur de ligne
ExcelApp.Rows[1].RowHeight := 15.75;
// Fusion de cellule
ExcelApp.Range['B3:D3'].Mergecells := True;
// Bordures
ExcelApp.Range['A14:C14'].Borders.Weight := xlThick; // Think line/ Dicke Linie
ExcelApp.Range['A14:C14'].Borders.Weight := xlThin; // Thin line Dünne Linie
ExcelApp.Range['B16:C20'].Font.Bold := True;
ExcelApp.Range['B16:C20'].Font.Size := 12;
ExcelApp.Range['B16:C20'].Font.Color := clRed;
ExcelApp.Range['B16:C20'].value := 'AAAAA' ;
ExcelApp.Range['B16:C20'].Font.Name := 'Tahoma';
//Alignement à droite
ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;
// horizontal-aligned text, horizontale Zentrierung
ExcelApp.Range['B14:C26'].HorizontalAlignment := xlHAlignCenter;
// Effacer la grille
ExcelApp.ActiveWindow.DisplayGridlines := False;
// Afficher Excel:
ExcelApp.Visible := True;
end;
{ ==================================================================== }
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Quitter Excel
if not VarIsEmpty(ExcelApp) then
begin
ExcelApp.DisplayAlerts := False;
ExcelApp.Quit;
end;
end;
{ ==================================================================== }
end. |