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
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ComObj,Winapi.ActiveX;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
oApplication, oDeskTop, oTable, oDocument,oCols,oCol,oRows: OLEvariant;
LoadParams : OLEVariant;
NomFichier : String;
i : integer;
Begin
oApplication := CreateOleObject('com.sun.star.ServiceManager');
oDeskTop := oApplication.createInstance('com.sun.star.frame.Desktop');
LoadParams := VarArrayCreate([0, -1], varVariant);
//NomFichier := ExtractFilePath(Paramstr(0))+'LO_WriterTemplate.odt';
NomFichier := 'c:\temp\OO\LO_WriterTemplate.odt';
NomFichier := StringReplace(NomFichier, '\', '/', [rfReplaceAll, rfIgnoreCase]);
oDocument := oDesktop.LoadComponentFromURL( 'file:///'+NomFichier, '_blank', 0, LoadParams);
oTable := oDocument.createInstance('com.sun.star.text.TextTable');
oTable.initialize(1,4);
oTable.setPropertyValue('HoriOrient', 7); // LEFT_AND_WIDTH
oTable.setPropertyValue('IsWidthRelative', true);
oTable.setPropertyValue('RelativeWidth', 50);
oTable.setPropertyValue('BackColor', $80); // fond bleu
// Montre le tableau ici:
oDocument.getText.insertTextContent(oDocument.getText.CreateTextCursor, oTable, False);
// Rempli les cellules de la première ligne
for i := 0 to 3 do oTable.getCellByPosition(i, 0).CreateTextCursor.setString(OleVariant('Col #'+IntToStr(i)));
// Acces aux Colonnes du tableau
oCols:=oTable.getColumns; // oCols : XTableColumns
// Pour chaque colonne du tableau:
for i := 0 to oCols.getcount-1 do
Begin
oCol := oCols.getByIndex(i); // oCol : xColumns
// !!! oCol.setPropertyValue('Width',100); // ==> ne fonctionne pas ! Exception !!
End;
// Ajoute une ligne au tableau
oRows := oTable.getRows;
oRows.insertByIndex(1,1);
// et renseigne la 1ere colonne
oTable.getCellByPosition(0, 1).CreateTextCursor.setString(OleVariant('Second line'));
ShowMessage('Press a key to close the document');
// Ferme le document
oDocument.dispose;
end;
end. |
Partager