Difference between revisions of "Documentation/DevGuide/Spreadsheets/Example: Adding a New Spreadsheet"
From Apache OpenOffice Wiki
< Documentation | DevGuide
m (typo) |
OOoWikiBot (Talk | contribs) m (FINAL VERSION FOR L10N) |
||
Line 5: | Line 5: | ||
|NextPage=Documentation/DevGuide/Spreadsheets/Example: Editing Spreadsheet Cells | |NextPage=Documentation/DevGuide/Spreadsheets/Example: Editing Spreadsheet Cells | ||
}} | }} | ||
− | {{DISPLAYTITLE:Example: Adding a New Spreadsheet}} | + | {{Documentation/DevGuideLanguages|Documentation/DevGuide/Spreadsheets/{{SUBPAGENAME}}}} |
+ | {{DISPLAYTITLE:Example: Adding a New Spreadsheet}} | ||
The following helper method opens a new spreadsheet document component. The method <code>getRemoteServiceManager()</code> retrieves a connection. Refer to chapter [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]] for additional information. | The following helper method opens a new spreadsheet document component. The method <code>getRemoteServiceManager()</code> retrieves a connection. Refer to chapter [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]] for additional information. | ||
Revision as of 10:20, 14 May 2009
The following helper method opens a new spreadsheet document component. The method getRemoteServiceManager()
retrieves a connection. Refer to chapter First Steps for additional information.
import com.sun.star.lang.XComponent; import com.sun.star.frame.XComponentLoader; import com.sun.star.beans.PropertyValue; ... protected XComponent newSpreadsheetComponent() throws java.lang.Exception { String loadUrl = "private:factory/scalc"; xRemoteServiceManager = this.getRemoteServiceManager(unoUrl); Object desktop = xRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", xRemoteContext); XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface( XComponentLoader.class, desktop); PropertyValue[] loadProps = new PropertyValue[0]; return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps); }
Our helper returns a com.sun.star.lang.XComponent interface for the recently loaded document. Now the XComponent is passed to the following method insertSpreadsheet()
to add a new spreadsheet to our document.
import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.sheet.XSpreadsheet; ... /** Inserts a new empty spreadsheet with the specified name. @param xSheetComponent The XComponent interface of a loaded document object @param aName The name of the new sheet. @param nIndex The insertion index. @return The XSpreadsheet interface of the new sheet. */ public XSpreadsheet insertSpreadsheet( XComponent xSheetComponent, String aName, short nIndex) { XSpreadsheetDocument xDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class, xSheetComponent); // Collection of sheets com.sun.star.sheet.XSpreadsheets xSheets = xDocument.getSheets(); com.sun.star.sheet.XSpreadsheet xSheet = null; try { xSheets.insertNewByName(aName, nIndex); xSheet = xSheets.getByName(aName); } catch (Exception ex) { } return xSheet; }
Content on this page is licensed under the Public Documentation License (PDL). |