1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //***************************************************************************
25cdf0e10cSrcweir // comment: Step 1: get the remote component context from the office
26cdf0e10cSrcweir //          Step 2: open an empty calc document
27cdf0e10cSrcweir //          Step 3: create cell styles
28cdf0e10cSrcweir //          Step 4: get the sheet an insert some data
29cdf0e10cSrcweir //          Step 5: apply the created cell syles
30cdf0e10cSrcweir //          Step 6: insert a 3D Chart
31cdf0e10cSrcweir //***************************************************************************
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
36cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.chart.XDiagram;
39cdf0e10cSrcweir import com.sun.star.chart.XChartDocument;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
42cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
43cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
48cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir import com.sun.star.lang.XComponent;
51cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
52cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
55cdf0e10cSrcweir import com.sun.star.uno.XInterface;
56cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable;
59cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
60cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
61cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
66cdf0e10cSrcweir import com.sun.star.table.XCell;
67cdf0e10cSrcweir import com.sun.star.table.XCellRange;
68cdf0e10cSrcweir import com.sun.star.table.XTableChart;
69cdf0e10cSrcweir import com.sun.star.table.XTableCharts;
70cdf0e10cSrcweir import com.sun.star.table.XTableChartsSupplier;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir public class SCalc  {
74cdf0e10cSrcweir 
main(String args[])75cdf0e10cSrcweir     public static void main(String args[]) {
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooooo
78cdf0e10cSrcweir         // call UNO bootstrap method and get the remote component context form
79cdf0e10cSrcweir         // the a running office (office will be started if necessary)
80cdf0e10cSrcweir         //***************************************************************************
81cdf0e10cSrcweir         XComponentContext xContext = null;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         // get the remote office component context
84cdf0e10cSrcweir         try {
85cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
86cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
87cdf0e10cSrcweir         } catch( Exception e) {
88cdf0e10cSrcweir             e.printStackTrace(System.err);
89cdf0e10cSrcweir             System.exit(1);
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooooo
93cdf0e10cSrcweir         // open an empty document. In this case it's a calc document.
94cdf0e10cSrcweir         // For this purpose an instance of com.sun.star.frame.Desktop
95cdf0e10cSrcweir         // is created. The desktop provides the XComponentLoader interface,
96cdf0e10cSrcweir         // which is used to open the document via loadComponentFromURL
97cdf0e10cSrcweir         //***************************************************************************
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         //Open document
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         //Calc
102cdf0e10cSrcweir         XSpreadsheetDocument myDoc = null;
103cdf0e10cSrcweir //        XCell oCell = null;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         System.out.println("Opening an empty Calc document");
106cdf0e10cSrcweir         myDoc = openCalc(xContext);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         //***************************************************************************
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo
112cdf0e10cSrcweir         // create cell styles.
113cdf0e10cSrcweir         // For this purpose get the StyleFamiliesSupplier and the the familiy
114cdf0e10cSrcweir         // CellStyle. Create an instance of com.sun.star.style.CellStyle and
115cdf0e10cSrcweir         // add it to the family. Now change some properties
116cdf0e10cSrcweir         //***************************************************************************
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         try {
119cdf0e10cSrcweir             XStyleFamiliesSupplier xSFS = (XStyleFamiliesSupplier)
120cdf0e10cSrcweir                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, myDoc);
121cdf0e10cSrcweir             XNameAccess xSF = (XNameAccess) xSFS.getStyleFamilies();
122cdf0e10cSrcweir             XNameAccess xCS = (XNameAccess) UnoRuntime.queryInterface(
123cdf0e10cSrcweir                 XNameAccess.class, xSF.getByName("CellStyles"));
124cdf0e10cSrcweir             XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
125cdf0e10cSrcweir                 UnoRuntime.queryInterface(XMultiServiceFactory.class, myDoc );
126cdf0e10cSrcweir             XNameContainer oStyleFamilyNameContainer = (XNameContainer)
127cdf0e10cSrcweir                 UnoRuntime.queryInterface(
128cdf0e10cSrcweir                 XNameContainer.class, xCS);
129cdf0e10cSrcweir             XInterface oInt1 = (XInterface) oDocMSF.createInstance(
130cdf0e10cSrcweir                 "com.sun.star.style.CellStyle");
131cdf0e10cSrcweir             oStyleFamilyNameContainer.insertByName("My Style", oInt1);
132cdf0e10cSrcweir             XPropertySet oCPS1 = (XPropertySet)UnoRuntime.queryInterface(
133cdf0e10cSrcweir                 XPropertySet.class, oInt1 );
134cdf0e10cSrcweir             oCPS1.setPropertyValue("IsCellBackgroundTransparent", new Boolean(false));
135cdf0e10cSrcweir             oCPS1.setPropertyValue("CellBackColor",new Integer(6710932));
136cdf0e10cSrcweir             oCPS1.setPropertyValue("CharColor",new Integer(16777215));
137cdf0e10cSrcweir             XInterface oInt2 = (XInterface) oDocMSF.createInstance(
138cdf0e10cSrcweir                 "com.sun.star.style.CellStyle");
139cdf0e10cSrcweir             oStyleFamilyNameContainer.insertByName("My Style2", oInt2);
140cdf0e10cSrcweir             XPropertySet oCPS2 = (XPropertySet)UnoRuntime.queryInterface(
141cdf0e10cSrcweir                 XPropertySet.class, oInt2 );
142cdf0e10cSrcweir             oCPS2.setPropertyValue("IsCellBackgroundTransparent", new Boolean(false));
143cdf0e10cSrcweir             oCPS2.setPropertyValue("CellBackColor",new Integer(13421823));
144cdf0e10cSrcweir         } catch (Exception e) {
145cdf0e10cSrcweir             e.printStackTrace(System.err);
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         //***************************************************************************
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooooo
151cdf0e10cSrcweir         // get the sheet an insert some data.
152cdf0e10cSrcweir         // Get the sheets from the document and then the first from this container.
153cdf0e10cSrcweir         // Now some data can be inserted. For this purpose get a Cell via
154cdf0e10cSrcweir         // getCellByPosition and insert into this cell via setValue() (for floats)
155cdf0e10cSrcweir         // or setFormula() for formulas and Strings
156cdf0e10cSrcweir         //***************************************************************************
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         XSpreadsheet xSheet=null;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         try {
162cdf0e10cSrcweir             System.out.println("Getting spreadsheet") ;
163cdf0e10cSrcweir             XSpreadsheets xSheets = myDoc.getSheets() ;
164cdf0e10cSrcweir             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
165cdf0e10cSrcweir                 XIndexAccess.class, xSheets);
166cdf0e10cSrcweir             xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
167cdf0e10cSrcweir                 XSpreadsheet.class, oIndexSheets.getByIndex(0));
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         } catch (Exception e) {
170cdf0e10cSrcweir             System.out.println("Couldn't get Sheet " +e);
171cdf0e10cSrcweir             e.printStackTrace(System.err);
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         System.out.println("Creating the Header") ;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         insertIntoCell(1,0,"JAN",xSheet,"");
179cdf0e10cSrcweir         insertIntoCell(2,0,"FEB",xSheet,"");
180cdf0e10cSrcweir         insertIntoCell(3,0,"MAR",xSheet,"");
181cdf0e10cSrcweir         insertIntoCell(4,0,"APR",xSheet,"");
182cdf0e10cSrcweir         insertIntoCell(5,0,"MAI",xSheet,"");
183cdf0e10cSrcweir         insertIntoCell(6,0,"JUN",xSheet,"");
184cdf0e10cSrcweir         insertIntoCell(7,0,"JUL",xSheet,"");
185cdf0e10cSrcweir         insertIntoCell(8,0,"AUG",xSheet,"");
186cdf0e10cSrcweir         insertIntoCell(9,0,"SEP",xSheet,"");
187cdf0e10cSrcweir         insertIntoCell(10,0,"OCT",xSheet,"");
188cdf0e10cSrcweir         insertIntoCell(11,0,"NOV",xSheet,"");
189cdf0e10cSrcweir         insertIntoCell(12,0,"DEC",xSheet,"");
190cdf0e10cSrcweir         insertIntoCell(13,0,"SUM",xSheet,"");
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         System.out.println("Fill the lines");
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         insertIntoCell(0,1,"Smith",xSheet,"");
196cdf0e10cSrcweir         insertIntoCell(1,1,"42",xSheet,"V");
197cdf0e10cSrcweir         insertIntoCell(2,1,"58.9",xSheet,"V");
198cdf0e10cSrcweir         insertIntoCell(3,1,"-66.5",xSheet,"V");
199cdf0e10cSrcweir         insertIntoCell(4,1,"43.4",xSheet,"V");
200cdf0e10cSrcweir         insertIntoCell(5,1,"44.5",xSheet,"V");
201cdf0e10cSrcweir         insertIntoCell(6,1,"45.3",xSheet,"V");
202cdf0e10cSrcweir         insertIntoCell(7,1,"-67.3",xSheet,"V");
203cdf0e10cSrcweir         insertIntoCell(8,1,"30.5",xSheet,"V");
204cdf0e10cSrcweir         insertIntoCell(9,1,"23.2",xSheet,"V");
205cdf0e10cSrcweir         insertIntoCell(10,1,"-97.3",xSheet,"V");
206cdf0e10cSrcweir         insertIntoCell(11,1,"22.4",xSheet,"V");
207cdf0e10cSrcweir         insertIntoCell(12,1,"23.5",xSheet,"V");
208cdf0e10cSrcweir         insertIntoCell(13,1,"=SUM(B2:M2)",xSheet,"");
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         insertIntoCell(0,2,"Jones",xSheet,"");
212cdf0e10cSrcweir         insertIntoCell(1,2,"21",xSheet,"V");
213cdf0e10cSrcweir         insertIntoCell(2,2,"40.9",xSheet,"V");
214cdf0e10cSrcweir         insertIntoCell(3,2,"-57.5",xSheet,"V");
215cdf0e10cSrcweir         insertIntoCell(4,2,"-23.4",xSheet,"V");
216cdf0e10cSrcweir         insertIntoCell(5,2,"34.5",xSheet,"V");
217cdf0e10cSrcweir         insertIntoCell(6,2,"59.3",xSheet,"V");
218cdf0e10cSrcweir         insertIntoCell(7,2,"27.3",xSheet,"V");
219cdf0e10cSrcweir         insertIntoCell(8,2,"-38.5",xSheet,"V");
220cdf0e10cSrcweir         insertIntoCell(9,2,"43.2",xSheet,"V");
221cdf0e10cSrcweir         insertIntoCell(10,2,"57.3",xSheet,"V");
222cdf0e10cSrcweir         insertIntoCell(11,2,"25.4",xSheet,"V");
223cdf0e10cSrcweir         insertIntoCell(12,2,"28.5",xSheet,"V");
224cdf0e10cSrcweir         insertIntoCell(13,2,"=SUM(B3:M3)",xSheet,"");
225cdf0e10cSrcweir 
226cdf0e10cSrcweir         insertIntoCell(0,3,"Brown",xSheet,"");
227cdf0e10cSrcweir         insertIntoCell(1,3,"31.45",xSheet,"V");
228cdf0e10cSrcweir         insertIntoCell(2,3,"-20.9",xSheet,"V");
229cdf0e10cSrcweir         insertIntoCell(3,3,"-117.5",xSheet,"V");
230cdf0e10cSrcweir         insertIntoCell(4,3,"23.4",xSheet,"V");
231cdf0e10cSrcweir         insertIntoCell(5,3,"-114.5",xSheet,"V");
232cdf0e10cSrcweir         insertIntoCell(6,3,"115.3",xSheet,"V");
233cdf0e10cSrcweir         insertIntoCell(7,3,"-171.3",xSheet,"V");
234cdf0e10cSrcweir         insertIntoCell(8,3,"89.5",xSheet,"V");
235cdf0e10cSrcweir         insertIntoCell(9,3,"41.2",xSheet,"V");
236cdf0e10cSrcweir         insertIntoCell(10,3,"71.3",xSheet,"V");
237cdf0e10cSrcweir         insertIntoCell(11,3,"25.4",xSheet,"V");
238cdf0e10cSrcweir         insertIntoCell(12,3,"38.5",xSheet,"V");
239cdf0e10cSrcweir         insertIntoCell(13,3,"=SUM(A4:L4)",xSheet,"");
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         //***************************************************************************
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooooo
244cdf0e10cSrcweir         // apply the created cell style.
245cdf0e10cSrcweir         // For this purpose get the PropertySet of the Cell and change the
246cdf0e10cSrcweir         // property CellStyle to the appropriate value.
247cdf0e10cSrcweir         //***************************************************************************
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         // change backcolor
250cdf0e10cSrcweir         chgbColor( 1 , 0, 13, 0, "My Style", xSheet );
251cdf0e10cSrcweir         chgbColor( 0 , 1, 0, 3, "My Style", xSheet );
252cdf0e10cSrcweir         chgbColor( 1 , 1, 13, 3, "My Style2", xSheet );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         //***************************************************************************
255cdf0e10cSrcweir 
256cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooooo
257cdf0e10cSrcweir         // insert a 3D chart.
258cdf0e10cSrcweir         // get the CellRange which holds the data for the chart and its RangeAddress
259cdf0e10cSrcweir         // get the TableChartSupplier from the sheet and then the TableCharts from it.
260cdf0e10cSrcweir         // add a new chart based on the data to the TableCharts.
261cdf0e10cSrcweir         // get the ChartDocument, which provide the Diagramm. Change the properties
262cdf0e10cSrcweir         // Dim3D (3 dimension) and String (the title) of the diagramm.
263cdf0e10cSrcweir         //***************************************************************************
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         // insert a chart
266cdf0e10cSrcweir 
267cdf0e10cSrcweir         Rectangle oRect = new Rectangle();
268cdf0e10cSrcweir         oRect.X = 500;
269cdf0e10cSrcweir         oRect.Y = 3000;
270cdf0e10cSrcweir         oRect.Width = 25000;
271cdf0e10cSrcweir         oRect.Height = 11000;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir         XCellRange oRange = (XCellRange)UnoRuntime.queryInterface(
274cdf0e10cSrcweir             XCellRange.class, xSheet);
275cdf0e10cSrcweir         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
276cdf0e10cSrcweir         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
277cdf0e10cSrcweir             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
278cdf0e10cSrcweir         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         CellRangeAddress[] oAddr = new CellRangeAddress[1];
281cdf0e10cSrcweir         oAddr[0] = myAddr;
282cdf0e10cSrcweir         XTableChartsSupplier oSupp = (XTableChartsSupplier)UnoRuntime.queryInterface(
283cdf0e10cSrcweir             XTableChartsSupplier.class, xSheet);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         XTableChart oChart = null;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         System.out.println("Insert Chart");
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         XTableCharts oCharts = oSupp.getCharts();
290cdf0e10cSrcweir         oCharts.addNewByName("Example", oRect, oAddr, true, true);
291cdf0e10cSrcweir 
292cdf0e10cSrcweir         // get the diagramm and Change some of the properties
293cdf0e10cSrcweir 
294cdf0e10cSrcweir         try {
295cdf0e10cSrcweir             oChart = (XTableChart) (UnoRuntime.queryInterface(
296cdf0e10cSrcweir                 XTableChart.class, ((XNameAccess)UnoRuntime.queryInterface(
297cdf0e10cSrcweir                             XNameAccess.class, oCharts)).getByName("Example")));
298cdf0e10cSrcweir             XEmbeddedObjectSupplier oEOS = (XEmbeddedObjectSupplier)
299cdf0e10cSrcweir                 UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, oChart);
300cdf0e10cSrcweir             XInterface oInt = oEOS.getEmbeddedObject();
301cdf0e10cSrcweir             XChartDocument xChart = (XChartDocument) UnoRuntime.queryInterface(
302cdf0e10cSrcweir                 XChartDocument.class,oInt);
303cdf0e10cSrcweir             XDiagram oDiag = (XDiagram) xChart.getDiagram();
304cdf0e10cSrcweir             System.out.println("Change Diagramm to 3D");
305cdf0e10cSrcweir             XPropertySet oCPS = (XPropertySet)UnoRuntime.queryInterface(
306cdf0e10cSrcweir                 XPropertySet.class, oDiag );
307cdf0e10cSrcweir             oCPS.setPropertyValue("Dim3D", new Boolean(true));
308cdf0e10cSrcweir             System.out.println("Change the title");
309cdf0e10cSrcweir             Thread.sleep(200);
310cdf0e10cSrcweir             XPropertySet oTPS = (XPropertySet)UnoRuntime.queryInterface(
311cdf0e10cSrcweir                 XPropertySet.class, xChart.getTitle() );
312cdf0e10cSrcweir             oTPS.setPropertyValue("String","The new title");
313cdf0e10cSrcweir             //oDiag.Dim3D();
314cdf0e10cSrcweir         } catch (Exception e){
315cdf0e10cSrcweir             System.err.println("Changin Properties failed "+e);
316cdf0e10cSrcweir             e.printStackTrace(System.err);
317cdf0e10cSrcweir         }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         System.out.println("done");
320cdf0e10cSrcweir         System.exit(0);
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir 
openCalc(XComponentContext xContext)323cdf0e10cSrcweir     public static XSpreadsheetDocument openCalc(XComponentContext xContext)
324cdf0e10cSrcweir     {
325cdf0e10cSrcweir         //define variables
326cdf0e10cSrcweir         XMultiComponentFactory xMCF = null;
327cdf0e10cSrcweir         XComponentLoader xCLoader;
328cdf0e10cSrcweir         XSpreadsheetDocument xSpreadSheetDoc = null;
329cdf0e10cSrcweir         XComponent xComp = null;
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         try {
332cdf0e10cSrcweir             // get the servie manager rom the office
333cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
334cdf0e10cSrcweir 
335cdf0e10cSrcweir             // create a new instance of the the desktop
336cdf0e10cSrcweir             Object oDesktop = xMCF.createInstanceWithContext(
337cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xContext );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir             // query the desktop object for the XComponentLoader
340cdf0e10cSrcweir             xCLoader = ( XComponentLoader ) UnoRuntime.queryInterface(
341cdf0e10cSrcweir                 XComponentLoader.class, oDesktop );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir             PropertyValue [] szEmptyArgs = new PropertyValue [0];
344cdf0e10cSrcweir             String strDoc = "private:factory/scalc";
345cdf0e10cSrcweir 
346cdf0e10cSrcweir             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs );
347cdf0e10cSrcweir             xSpreadSheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
348cdf0e10cSrcweir                 XSpreadsheetDocument.class, xComp);
349cdf0e10cSrcweir 
350cdf0e10cSrcweir         } catch(Exception e){
351cdf0e10cSrcweir             System.err.println(" Exception " + e);
352cdf0e10cSrcweir             e.printStackTrace(System.err);
353cdf0e10cSrcweir         }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         return xSpreadSheetDoc;
356cdf0e10cSrcweir     }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 
insertIntoCell(int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)359cdf0e10cSrcweir     public static void insertIntoCell(int CellX, int CellY, String theValue,
360cdf0e10cSrcweir                                       XSpreadsheet TT1, String flag)
361cdf0e10cSrcweir     {
362cdf0e10cSrcweir         XCell xCell = null;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir         try {
365cdf0e10cSrcweir             xCell = TT1.getCellByPosition(CellX, CellY);
366cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
367cdf0e10cSrcweir             System.err.println("Could not get Cell");
368cdf0e10cSrcweir             ex.printStackTrace(System.err);
369cdf0e10cSrcweir         }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir         if (flag.equals("V")) {
372cdf0e10cSrcweir             xCell.setValue((new Float(theValue)).floatValue());
373cdf0e10cSrcweir         } else {
374cdf0e10cSrcweir             xCell.setFormula(theValue);
375cdf0e10cSrcweir         }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     }
378cdf0e10cSrcweir 
chgbColor( int x1, int y1, int x2, int y2, String template, XSpreadsheet TT )379cdf0e10cSrcweir     public static void chgbColor( int x1, int y1, int x2, int y2,
380cdf0e10cSrcweir                                   String template, XSpreadsheet TT )
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir         XCellRange xCR = null;
383cdf0e10cSrcweir         try {
384cdf0e10cSrcweir             xCR = TT.getCellRangeByPosition(x1,y1,x2,y2);
385cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
386cdf0e10cSrcweir             System.err.println("Could not get CellRange");
387cdf0e10cSrcweir             ex.printStackTrace(System.err);
388cdf0e10cSrcweir         }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir         XPropertySet xCPS = (XPropertySet)UnoRuntime.queryInterface(
391cdf0e10cSrcweir             XPropertySet.class, xCR );
392cdf0e10cSrcweir 
393cdf0e10cSrcweir         try {
394cdf0e10cSrcweir             xCPS.setPropertyValue("CellStyle", template);
395cdf0e10cSrcweir         } catch (Exception e) {
396cdf0e10cSrcweir             System.err.println("Can't change colors chgbColor" + e);
397cdf0e10cSrcweir             e.printStackTrace(System.err);
398cdf0e10cSrcweir         }
399cdf0e10cSrcweir     }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir }
402