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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer,
Excel2000, StdCtrls;
const
cstMonFichier='c:\temp\Test.xls';
StartRow=1; // numéro de la premiére ligne d'information du fichier Excel
type
TForm1 = class(TForm)
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure ExcelApplication1WorkbookBeforeClose(ASender: TObject;
const Wb: _Workbook; var Cancel: WordBool);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
lcid : integer; // page code pour Excel
bExcelIsConnected : boolean; // si la connection sur Excel est active
public
{ Déclarations publiques }
CurrentFilename : String; // fichier en cours de traitement
Procedure XlOpen;
Function XlOuvreFichier(FileName : TFileName):Boolean;
Procedure ReadCurrentWorkSheet(SheetNumber:Byte);
procedure ItereFichiers;
procedure FermeXLS(FileName : TFileName);
Procedure XlQuit;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Procedure TForm1.XlOpen;
begin
// Crée une nouvelle instance du serveur.
ExcelApplication1.ConnectKind := ckNewInstance;
try
ExcelApplication1.Disconnect;
ExcelApplication1.Connect;
except on E: Exception do
begin
E.Message := 'Excel indisponible.';
MessageDlg(E.Message, mtInformation,[mbOk], 0);
raise;
end;
end;
ExcelApplication1.Visible[0] := True;
bExcelIsConnected := True;
end;
Function TForm1.XlOuvreFichier(FileName : TFileName):Boolean;
begin
Try
Result:=True;
// Ouvre un fichier et le connecte au classeur ( avec les options par défaut )
ExcelWorkbook1.ConnectTo( ExcelApplication1.WorkBooks.Open(FileName,0,False,5,VarEmpty,VarEmpty,True,xlWindows,VarEmpty,
False,False,VarEmpty,VarEmpty,lcid));
except on E: Exception do
begin
Result:=False;
E.Message := 'Erreur d''ouverture du fichier Excel.';
MessageDlg(E.Message, mtInformation,[mbOk], 0);
end;
end;
end;
Procedure TForm1.ReadCurrentWorkSheet(SheetNumber:Byte);
var ColNumber,RowNumber : Integer;
S,L : String;
Procedure Convert;
{test le format de la cellule Excel, sinon probléme de convertion du type date-heure vers type flottant }
begin
L:=ExcelWorksheet1.Cells.Item[RowNumber,ColNumber].NumberFormat;
// un cellule vide renvoie 0 ( zéro ) vers une String Pascal
if (L='Standard') Or (L='Nombre')
then
begin
L:=ExcelWorksheet1.Cells.Item[RowNumber,ColNumber];
Memo2.Lines.Add('Format Standart '+ ExtractFileName(CurrentFileName)+' Feuille numéro : '+intTostr(SheetNumber)+' Ligne :'+intTostr(RowNumber)+' Colonne '+intTostr(ColNumber)) ;
end
else
if (L='hh:mm') or (L='[h]:mm:ss')
then L:= TimeToStr(ExcelWorksheet1.Cells.Item[RowNumber,ColNumber])
else Memo2.Lines.Add('Format inconnu '+ ExtractFileName(CurrentFileName)+' Feuille numéro : '+intTostr(SheetNumber)+' Ligne :'+intTostr(RowNumber)+' Colonne '+intTostr(ColNumber)) ;
end;
begin
try
RowNumber:=StartRow;
L:=ExcelWorksheet1.Cells.Item[RowNumber,1];
While L <> '' do // si L est vide , c'est la fin du fichier.
begin
S:='';
for ColNumber:=1 to 10 do
begin
Convert;
end;
Inc(RowNumber);
L:=ExcelWorksheet1.Cells.Item[RowNumber,1]; // lit la prochaine ligne
end;
except on E: Exception do
begin
E.Message := 'Erreur de lecture du fichier Excel.';
MessageDlg(E.Message, mtInformation,
[mbOk], 0);
end;
end;
end;
procedure TForm1.FermeXLS(FileName : TFileName);
{ferme lie fichier Excel aprés traitement}
begin
try
// se déconnect de la feuille
ExcelWorksheet1.Disconnect;
// Ferme et se déconnect du classeur
ExcelApplication1.WorkBooks.Close(lcid);
ExcelWorkbook1.Disconnect;
except on E: Exception do
begin
E.Message := 'Erreur lors de la fermeture du fichier Excel.';
MessageDlg(E.Message, mtInformation,
[mbOk], 0);
{raise;}
end;
end;
end;
Procedure TForm1.XlQuit;
begin
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ExcelApplication1.Quit;
bExcelIsConnected := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
bExcelIsConnected := False;
XlOpen;
If bExcelIsConnected = False
then Close;
lcid := GetUserDefaultLCID;
end;
procedure TForm1.ExcelApplication1WorkbookBeforeClose(ASender: TObject;
const Wb: _Workbook; var Cancel: WordBool);
begin
// Stop les alertes pour éviter la boîte de dialogue demandant de sauver avant de quitter.
ExcelApplication1.DisplayAlerts[lcid] := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Iterefichiers;
end;
procedure TForm1.ItereFichiers;
const
NombreFeuilleExcel=3; //le fichier Excel comporte n feuilles.Chaque feuille
// contient un nombre de colonne différente
Var nbSheet :Integer;
//FileList : TStrings; // liste des fichiers Excel à traiter
begin
// for I:=0 to FileList.Count-1 do
// begin
CurrentFileName:=cstMonFichier;
if XLOuvreFichier(CurrentFileName) then
begin
for nbSheet:=1 to NombreFeuilleExcel do
begin
//Memo1.Lines.Add(FileList.Strings[I]+' Feuille numéro : '+IntToStr(nbSheet));
Memo1.Lines.Add(CurrentFileName+' Feuille numéro : '+IntToStr(nbSheet));
// se déconnecte de la feuille
ExcelWorksheet1.Disconnect;
// Se connecte à la première feuille
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[nbSheet] as _WorkSheet);
ExcelWorksheet1.Activate;
ReadCurrentWorkSheet(nbSheet);
end;
FermeXLS(CurrentFileName);
end;
//end;
end;
end. |
Partager