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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, Excel2000, ActiveX, comobj, StdCtrls;
type
TForm1 = class(TForm)
OpenXls: TButton;
btnCloseExcel: TButton;
btnWriteCell: TButton;
btnReadCell: TButton;
XlsApp: TExcelApplication;
edWCell: TEdit;
edRCell: TEdit;
procedure OpenXlsClick(Sender: TObject);
procedure btnCloseExcelClick(Sender: TObject);
procedure btnWriteCellClick(Sender: TObject);
procedure btnReadCellClick(Sender: TObject);
private
{ Déclarations privées }
function XlsInit(var lcid: integer; var XlsApp: TExcelApplication; var XlsWork: _workbook; var XlsPage: _Worksheet;
sFile, sNameWork: string; bVisible: boolean): boolean;
procedure XlsClose(XlsApp: TExcelApplication; XlsWork: _workbook; bSave: boolean);
function Xls_RCell(Line, Col: integer): variant;
procedure Xls_WCell(Line, Col: integer; Value: variant);
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
lcid: integer;
XlsWBook: _workbook; // classeur
XlsPage: _Worksheet; // Feuille
////////////////
// lancer / ouvrir fichier XLS
function TForm1.XlsInit(var lcid: integer; var XlsApp: TExcelApplication; var XlsWork: _workbook; var XlsPage: _Worksheet;
sFile, sNameWork: string; bVisible: boolean): boolean;
begin
Result := true;
CoInitialize(nil);
TRY
lcid := GetUserDefaultLCID; // Identifiant local
XlsApp.Visible[lcid]:= bVisible; // lance Excel
XlsWork := XlsApp.Workbooks.Open(sFile, False,False, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,0);
EXCEPT
ShowMessage('Excel indisponible, ou fichier occupé... Ré-essayer plus tard !');
if XlsWork <> nil then
XlsApp.Quit;
Result := false;
Exit;
END;
XlsPage := XlsWork.Worksheets[sNameWork] as _worksheet;
end;
///////////////////////////
procedure TForm1.OpenXlsClick(Sender: TObject);
var
sFile: string;
bVisibleXls: boolean;
sNameFeuille: string;
begin
// Lancer Excel + Open Xls
sFile := ExtractFilePath(Application.ExeName) + 'Classeur1.XLS';
bVisibleXls := true; // Voir Excel
sNameFeuille := 'Feuil1';
XlsInit(lcid, XlsApp, XlsWBook, XlsPage, sFile, sNameFeuille, bVisibleXls);
end;
procedure TForm1.btnCloseExcelClick(Sender: TObject);
var
bSaveXls: boolean;
begin
// Fermer Excel
bSaveXls := false; // pas de sauvegarde
XlsClose(XlsApp, XlsWBook, bSaveXls);
end;
procedure TForm1.btnWriteCellClick(Sender: TObject);
var
iLine, iCol: integer;
oValue: variant;
begin
//Insére du texte dans la première cellule [1,1]
iLine := 1;
iCol := 1;
ovalue := edWCell.Text;
Xls_WCell(iLine, iCol, oValue);
end;
procedure TForm1.btnReadCellClick(Sender: TObject);
var
iLine, iCol: integer;
begin
// lecture d'une cellule
iLine := 1;
iCol := 1;
edRCell.Text := Xls_RCell(iLine, iCol);
end;
// fermer Excel
procedure TForm1.XlsClose(XlsApp: TExcelApplication; XlsWork: _workbook; bSave: boolean);
begin
XlsWork.Close(bSave, emptyparam, emptyparam, lcid);
XlsApp.Quit;
end;
// Lecture d'une cellule
function TForm1.Xls_RCell(Line, Col: integer): variant;
begin
Result := XlsPage.Cells.Item[Line, Col].Value;
end;
// Ecriture dans une cellule
procedure TForm1.Xls_WCell(Line, Col: integer; Value: variant);
begin
if Value <> NULL then
XlsPage.Cells.Item[Line, Col] := Value;
end;
end. |
Partager