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
| procedure TForm1.Button1Click(Sender: TObject);
var OpenOffice, Bureau, Document,Locale,Formats : Variant;
properties,CoreServ,prop : Variant;
vSheet,vCell : Variant;
n,ifdate : Integer;
WString : String;
OO_FileName : WideString;
test : TColumn;
// fonction pour transformer un nom de fichier DOS
function FileToURL(F : String) : String;
var i : Word;
R : String;
begin
R:='file:///'+F;
// transforme le nom du fichier en adresse URL
while (Pos('\',R)>0) do {les \ en /}
begin
i:=Pos('\',R);
Delete(R,i,1);
Insert('/',R,i);
end;
while (Pos(' ',R)>0) do {les espaces en %20}
begin
i:=Pos(' ',R);
Delete(R,i,1);
Insert('%20',R,i);
end;
result:=R;
end;
begin
// Appel du service OpenOffice
if (VarIsEmpty(OpenOffice) or VarIsNull(OpenOffice)) then
OpenOffice:=CreateOleObject('com.sun.star.ServiceManager');
if not(VarIsEmpty(OpenOffice) or VarIsNull(OpenOffice)) then
begin
if (VarIsEmpty(Bureau) or VarIsNull(Bureau)) then
Bureau:= OpenOffice.CreateInstance('com.sun.star.frame.Desktop');
end;
if (VarIsEmpty(Bureau) or VarIsNull(Bureau)) then
begin
MessageDlg('Le service Open Office n''est pas ouvert',mtError,[mbOk],0);
Exit;
end;
//Voir ou ne pas voir OpenOffice Calc that is the question
properties:=VarArrayCreate([0,0],varVariant);
CoreServ:= OpenOffice.CreateInstance('com.sun.star.reflection.CoreReflection');
CoreServ.forName('com.sun.star.beans.PropertyValue').CreateObject(prop);
prop.name:='Hidden';
prop.Value:=False; // ou true dépendant de votre réponse
properties[0]:=prop;
// on y arrive enfin !!!!!!!!!!!!!!!!!!!!!!!!!!!
if FileExists('C:\MESSAROUND\test.ods')
then
OO_FileName:=FileToURL('C:\MESSAROUND\test.ods')
else
OO_FileName:='private:factory/scalc';
// Ouverture du document
Document := Bureau.LoadComponentFromURL(OO_filename,
'_blank',
0,
properties);
if (VarIsEmpty(Document) or VarIsNull(Document)) then
begin
MessageDlg('Le fichier n''est pas ouvert',mtError,[mbOk],0);
Exit;
end;
//Recherche de la feuille par son nom
vSheet:=Document.getSheets.getByName('Feuille1');
// ou par son index
//vSheet:=Document.getSheets.getByIndex(0);
if (VarIsEmpty(vSheet) or VarIsNull(vSheet)) then
begin
MessageDlg('La Feuille n''est pas ouverte',mtError,[mbOk],0);
Exit;
end;
Locale:=Document.CharLocale;
{pour changer la Localisation , utilisez ce qui suit }
{CoreServ.forName('com.sun.star.lang.Locale').createObject(Locale);
Locale.Country:='FR';
Locale.Language:='fr';
Locale.Variant := '';
}
// ici je récupère un indice pour les cellules dans lequelles je mettrai des dates
//et comme je ne veut pas de ce bon vieux JJ/MM/AAAA j'en crée un inexistant ?
Formats:=Document.GetNumberFormats();
iFDate:=Formats.QueryKey('MMM AAAA JJ',Locale,TRUE);
if IfDate=-1 then
begin
ifDate:=Formats.AddNew('MMM AAAA JJ',Locale);
if ifDate=-1 then ifDate:=0;
end;
// pour répondre a la question des accents
vcell:=vSheet.getCellByPosition(1,1); // obtention de la cellule ATTENTION Base 0
vCell.SetString(UTF8Decode('Testée et approuvée'));
//Mettre un chiffre, rien de bien sorcier
vcell:=vSheet.getCellByPosition(2,1);
vCell.Value:=10;
//Mettre un chaine de caractère variables ATTENTION un WideString
WString:='0001';
vcell:=vSheet.getCellByPosition(3,1);
vCell.SetString(WString);
//Mettre une date
vcell:=vSheet.getCellByPosition(5,1);
vCell.NumberFormat:=ifdate;
vCell.Value:=now;
// fermer le service (ne ferme pas openOffice si visible)
OpenOffice:=unAssigned; |
Partager