/************************************************************************** /* This class shows how to create and use a spreadsheet. /* (copied and modified from the OOo SDK) /* /* Copyright (c) 2003 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.oo; import com.sun.star.bridge.*; import com.sun.star.uno.*; import com.sun.star.sheet.*; import com.sun.star.table.*; import com.sun.star.lang.*; import com.sun.star.connection.*; import com.sun.star.comp.helper.*; import com.sun.star.beans.*; import com.sun.star.frame.*; import com.sun.star.util.*; /** This class shows how to create and use a spreadsheet. (copied and modified from various OOo SDK samples) @version $Revision: 1.1 $ @author $Author: Bablokb $ */ public class JavaSpreadsheet { private static final String UNO_URL = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"; private static final String TABLE_NAME = "Java"; private static final String DOCUMENT_FILENAME = "file:///tmp/oo-java.sxc"; private XComponentContext iRemoteContext = null; private XMultiComponentFactory iRemoteServiceManager = null; private XComponent iSpreadsheetComponent = null; private XSpreadsheetDocument iSpreadsheetDocument = null; private XModel iSpreadsheetModel = null; private XSpreadsheets iSpreadsheets = null; ////////////////////////////////////////////////////////////////////////////// /** Constructor. */ public JavaSpreadsheet() throws java.lang.Exception { getRemoteContext(); iRemoteServiceManager = iRemoteContext.getServiceManager(); } ////////////////////////////////////////////////////////////////////////////// /** Query the remote context. */ private void getRemoteContext() throws java.lang.Exception { try { XComponentContext initContext = Bootstrap.createInitialComponentContext(null); XMultiComponentFactory manager = initContext.getServiceManager(); Object urlResolver = manager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver",initContext); XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime. queryInterface(XUnoUrlResolver.class,urlResolver); Object initialObject = xUnoUrlResolver.resolve(UNO_URL); XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class,initialObject); Object context = xPropertySet.getPropertyValue("DefaultContext"); iRemoteContext = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,context); } catch(NoConnectException nce) { System.err.println( "No process listening on the resource" ); nce.printStackTrace(); throw nce; } catch(DisposedException de) { iRemoteContext = null; throw de; } } ////////////////////////////////////////////////////////////////////////////// /** Create new spreadsheet and fill it with some data. */ private void createSpreadsheet() throws java.lang.Exception { Object desktop = iRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", iRemoteContext); XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, desktop); PropertyValue[] loadProps = new PropertyValue[0]; iSpreadsheetComponent = componentLoader.loadComponentFromURL( "private:factory/scalc", "_blank", 0, loadProps); iSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface( XSpreadsheetDocument.class, iSpreadsheetComponent); iSpreadsheets = iSpreadsheetDocument.getSheets(); iSpreadsheets.insertNewByName(TABLE_NAME, (short)0); iSpreadsheetModel = (XModel) UnoRuntime.queryInterface(XModel.class,iSpreadsheetComponent); } ////////////////////////////////////////////////////////////////////////////// /** Fill spreadsheet with some data. */ private void useSpreadsheet() throws java.lang.Exception { Object sheet = iSpreadsheets.getByName(TABLE_NAME); XSpreadsheet spreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( XSpreadsheet.class, sheet); XCell cell = spreadsheet.getCellByPosition(0, 0); cell.setValue(42); cell = spreadsheet.getCellByPosition(0, 1); cell.setValue(42); cell = spreadsheet.getCellByPosition(0, 2); cell.setFormula("=sum(A1:A2)"); XPropertySet cellProps = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, cell); cellProps.setPropertyValue("CellStyle", "Result"); XController spreadsheetController = iSpreadsheetModel.getCurrentController(); XSpreadsheetView spreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface( XSpreadsheetView.class,spreadsheetController); spreadsheetView.setActiveSheet(spreadsheet); } ////////////////////////////////////////////////////////////////////////////// /** Store spreadsheet. */ private void storeSpreadsheet() throws java.lang.Exception { XStorable store = ( XStorable ) UnoRuntime.queryInterface(XStorable.class,iSpreadsheetModel); PropertyValue[] prop = new PropertyValue[2]; prop[0] = new PropertyValue(); prop[0].Name = "Overwrite"; prop[0].Value = new Boolean(true); prop[1] = new PropertyValue(); prop[1].Name = "FilterName"; prop[1].Value = "StarOffice XML (Calc)"; store.storeAsURL(DOCUMENT_FILENAME,prop); System.out.println("document saved"); } ////////////////////////////////////////////////////////////////////////////// /** Close spreadsheet. */ private void closeDocument() throws java.lang.Exception { iSpreadsheetComponent.dispose(); System.out.println("document closed"); } ////////////////////////////////////////////////////////////////////////////// /** Main entry point. */ public static void main(String[] args) { try { JavaSpreadsheet js = new JavaSpreadsheet(); js.createSpreadsheet(); js.useSpreadsheet(); js.storeSpreadsheet(); js.closeDocument(); } catch (java.lang.Exception e){ e.printStackTrace(); } System.exit(0); } }