NOTE: This page has been translated automatically from Russian to English. Original page.
1C 8: Working with MS Excel files using OpenOffice / LibreOffice
There are situations when it is impossible to install Excel on the client side, and need to load data into * .xls files database. Salvation is a free OpenOffice (or LibreOffice, since work through COM with them is exactly the same).
Below is the loading and unloading procedures.
ZagruzitTZizFayla function (PutKFaylu)
// OpenOffice Calc ////////////////////////////////////////////// ////////////////////////////////////////////////
Attempt
ServiceManager = New COMObekt ( "com.sun.star.ServiceManager");
An exception
Warning (OpisanieOshibki () + + Simvoly.PS "program OpenOffice \ LibreOffice is not installed on the computer!");
Return Undefined;
KonetsPopytki;
Desktop = ServiceManager.createInstance ( "com.sun.star.frame.Desktop");
Properties = ServiceManager.Bridge_GetStruct ( "com.sun.star.beans.PropertyValue");
Svoystva.Name = "AsTemplate";
Svoystva.Value = True;
Args = New COMSafeArray ( "VT_VARIANT", 2);
Args.SetValue (0 Properties);
Properties1 = ServiceManager.Bridge_GetStruct ( "com.sun.star.beans.PropertyValue");
Svoystva1.Name = "Hidden";
Svoystva1.Value = True;
Args.SetValue (1 properties1);
Document = Desktop.LoadComponentFromURL (PrivestiFaylKURL (PutKFaylu), "_blank", 0, Args);
Sheets = Document.getSheets ();
Sheet = Sheets.getByIndex (tekList-1);
lokTabZnach = New TablitsaZnacheny;
begCol = 0;
begRow = 0;
endCol = Sheet.Data.GetLength (6) - 1;
endRow = Sheet.Data.GetLength () - 1;
Nkole = 0;
lokTabZnach.Kolonki.Ochistit ();
COP = New KvalifikatoryStroki (50);
Array = new Array;
Massiv.Dobavit (type ( "string"));
OpisanieTipovStroka = New OpisanieTipov (Array, CC);
While Nkole <= endCol Cycle
ImyaKol = "K" + String (Nkole + 1);
lokTabZnach.Kolonki.Dobavit (ImyaKol, OpisanieTipovStroka);
Nkole Nkole = + 1;
KonetsTsikla;
Range = Sheet.getCellRangeByPosition (begCol, begRow, endCol, endRow);
MassivKom = New COMSafeArray ( "VT_VARIANT", Range.Columns.Count, Range.Rows.Count);
MassivKom = Range.getDataArray ();
tmpProstoyMassiv MassivKom.Vygruzit = ();
For each array of tmpProstoyMassiv Cycle
um = 0;
NovStroka lokTabZNach.Dobavit = ();
For each cycle of the array of ElementMassiva
NovStroka [IT] = ElementMassiva;
um um + = 1;
KonetsTsikla;
KonetsTsikla;
Return lokTabZnach;
KonetsFunktsii As can be seen from the function ZagruzitTZizFayla - just read the whole area of file data, so with the speed of everything is fine. At the exit, we have finished a table of values. In this case, each column defined type string (50), but you can not do this, just need it if you are going, for example, put it into a temporary table.
VygruzitTZvFayl procedure (TablitsaRezultata)
// OpenOffice Calc ////////////////////////////////////////////// /////////////////////////////
Attempt
ServiceManager = New COMObekt ( "com.sun.star.ServiceManager");
An exception
Warning (OpisanieOshibki () + + Simvoly.PS "of OpenOffice is not installed on the computer!");
Return;
KonetsPopytki;
Desktop = ServiceManager.createInstance ( "com.sun.star.frame.Desktop");
Properties = ServiceManager.Bridge_GetStruct ( "com.sun.star.beans.PropertyValue");
Svoystva.Name = "AsTemplate";
Svoystva.Value = True;
Args = New COMSafeArray ( "VT_VARIANT", 3);
Args.SetValue (0 Properties);
Properties1 = ServiceManager.Bridge_GetStruct ( "com.sun.star.beans.PropertyValue");
Svoystva1.Name = "Hidden";
Svoystva1.Value = True;
Args.SetValue (1 properties1);
Document = Desktop.LoadComponentFromURL ( "private: factory / scalc", "_blank", 0, Args);
Sheets = Document.getSheets ();
Sheet = Sheets.getByIndex (0);
mItKol = 0;
For each count of TablitsaRezultata.Kolonki Cycle
Sheet.getCellByPosition (mItKol, 0) .setString (kol.Imya);
mItKol mItKol = + 1;
KonetsTsikla
VsegoStrok TablitsaRezultata.Kolichestvo = ();
VsegoKolonok TablitsaRezultata.Kolonki.Kolichestvo = ();
MassivKOM = New COMSafeArray ( "VT_VARIANT", VsegoStrok, VsegoKolonok);
Itkol = 0;
itstr = 0;
For each page of TablitsaRezultata Cycle
For each count of TablitsaRezultata.Kolonki Cycle
strRez SokrLP = (p [kol.Imya]);
MassivKOM.SetValue (itstr, Itkol, strRez);
Itkol Itkol = + 1;
KonetsTsikla;
Itkol = 0;
itstr = (itstr VsegoStrok = 0, itstr + 1?);
KonetsTsikla;
begCol = 0;
begRow = 1;
VsegoKolonok endCol = 1;
endRow = VsegoStrok;
Range = Sheet.getCellRangeByPosition (begCol, begRow, endCol, endRow);
Range.setDataArray (MassivKOM);
//Document.storeToURL(PrivestiFaylKURL(PutKFaylu),Args);
Svoystva2 = ServiceManager.Bridge_GetStruct ( "com.sun.star.beans.PropertyValue");
Svoystva2.Name = "FilterName";
Svoystva2.Value = "MS Excel 97";
Args.SetValue (2 Svoystva2);
... Document.GetCurrentController () GetFrame () GetContainerWindow () SetVisible (the Truth);
//Document.storeToURL(PrivestiFaylKURL(FaylKartoteki),Args);
KonetsProtsedury Procedure VygruzitTZvFayl retains any value table in MS Excel file. In the current example, open OpenOffice / LibreOffice Calc c data transmitted by a table of values. If you uncomment the line:
//Document.storeToURL(PrivestiFaylKURL(PutKFaylu),Args);
.................................................. .
//Document.storeToURL(PrivestiFaylKURL(FaylKartoteki),Args); and comment out
... Document.GetCurrentController () GetFrame () GetContainerWindow () SetVisible (the Truth); while adding PutKFaylu parameter, the data is written in the file without opening Calc`a. If you pay attention to the preservation, the double challenge thrown at once Document.storeToURL - necessary for the correct preservation in MS Excel format.
PrivestiFaylKURL function (PutKFaylu)
PutKFaylu = StrZamenit (PutKFaylu, "", "20%");
PutKFaylu = StrZamenit (PutKFaylu, "\", "/");
Return "file: /" + "/ localhost /" + PutKFaylu;
Konetsfunktsii Function PrivestiFaylKURL simply leads the way to a form required OpenOffice / LibreOffice.
That's all. How did you make - work with Calc`om not difficult per se with Microsoft Excel.