import java.util.*; import com.sun.star.frame.XStorable; // base interface import com.sun.star.uno.XInterface; // access the implementations via names import com.sun.star.comp.servicemanager.ServiceManager; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.connection.XConnector; import com.sun.star.connection.XConnection; import com.sun.star.uno.IBridge; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XInterface; import com.sun.star.uno.XNamingService; import com.sun.star.uno.Type; // staroffice interfaces to provide desktop and componentloader // and components i.e. spreadsheets, writerdocs etc. import com.sun.star.frame.XDesktop; import com.sun.star.frame.XComponentLoader; // additional classes required import com.sun.star.sheet.*; import com.sun.star.container.*; import com.sun.star.table.*; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.*; import com.sun.star.style.*; import com.sun.star.lang.*; import com.sun.star.text.*; import com.sun.star.chart.*; import com.sun.star.document.*; import com.sun.star.awt.Rectangle; //import com.sun.star.lib.util.*; public class tst609 { static String neededServices[] = new String[] { "com.sun.star.comp.servicemanager.ServiceManager", "com.sun.star.comp.loader.JavaLoader", "com.sun.star.comp.connections.Connector", "com.sun.star.comp.connections.Acceptor" }; XConnection xConnection; XIndexAccess oIndexSheets = null; XSpreadsheets oSheets=null; XSpreadsheet oSheet =null; static int cnt=0; XComponent aDoc = null; Vector vt = null; public tst609() { XMultiServiceFactory xMSF = null; XSpreadsheetDocument myDoc = null; XCell oCell = null; int maxR = 0; int maxC = 0; //the connection string to connet the office String connectStr = "socket, host=localhost,port=8100"; // create connection(s) and get multiservicefactory System.out.println( "getting MultiServiceFactory" ); try { xMSF = connect( connectStr ); } catch( Exception Ex ) { System.out.println( "Couldn't get MSF"+ Ex ); } try { System.out.println("Opening an empty Calc document"); myDoc = openCalc(xMSF); System.out.println("Getting spreadsheet") ; XSpreadsheets oSheets = myDoc.getSheets() ; XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, oSheets); System.out.println("No of sheets:"+oIndexSheets.getCount()); int sz= oIndexSheets.getCount()-1; //aDoc.dispose(); for(int si=0;si<=sz;++si) { oSheet = (XSpreadsheet) oIndexSheets.getByIndex(si); XCellRangesQuery xi = (XCellRangesQuery) UnoRuntime.queryInterface(XCellRangesQuery.class, oSheet); XSheetCellRanges ranges = xi.queryEmptyCells(); maxR = 0; maxC = 0; CellRangeAddress[] add = ranges.getRangeAddresses(); for(int i=0;i"+maxR+" --->"+maxC); for(int i=0;i"+oCell.getValue()); if(ctype.equals(CellContentType.FORMULA)) System.out.println("fmla->"+oCell.getFormula()); System.out.println("fmla->"+oCell.getFormula()); if(ctype.equals(CellContentType.TEXT)) System.out.println("str->"+oCell.getFormula()); } } } }catch(Exception exx){ System.out.println("exxception here"); exx.printStackTrace(); } finally{ try{ aDoc.dispose(); } catch(Exception e){ e.printStackTrace();} } } // finish method main public XMultiServiceFactory connect ( String connectStr ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException, Exception { XMultiServiceFactory xMSF = null; // initial serviceManager System.out.println("444444444"); ServiceManager serviceManager = new ServiceManager(); serviceManager.addFactories( neededServices ); System.out.println("222222222"); // create a connector, so that it can contact the office Object connector = serviceManager.createInstance( "com.sun.star.connection.Connector" ); XConnector connector_xConnector = (XConnector)UnoRuntime.queryInterface( XConnector.class, connector ); // connect to the office xConnection = connector_xConnector.connect( connectStr ); // now map the initial object, the parts in **** may change, when the bridge is usable as a service // ************* String rootOid = "classic_uno"; IBridge iBridge = UnoRuntime.getBridgeByName( "java", null, "remote", null, new Object[]{"iiop", xConnection, null}); Object rInitialObject = iBridge.mapInterfaceFrom( rootOid, XInterface.class ); //if( rInitialObject != null ) { // System.err.println( "got the remote object" ); //} System.out.println("5555555"); XNamingService rName = (XNamingService)UnoRuntime.queryInterface( XNamingService.class, rInitialObject ); if( rName != null ) { System.err.println( "got the remote naming service !" ); Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" ); xMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr ); } return ( xMSF ); } /// finish connect //public void store(){} public synchronized XSpreadsheetDocument openCalc(XMultiServiceFactory oMSF) { //define variables XSpreadsheetDocument oDoc = null; XCell oCell = null; int maxC=0; int maxR=0; XInterface oInterface; XDesktop oDesktop; XComponentLoader oCLoader; try { oInterface = (XInterface) oMSF.createInstance( "com.sun.star.frame.Desktop" ); oDesktop = ( XDesktop ) UnoRuntime.queryInterface( XDesktop.class, oInterface ); oCLoader = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); //PropertyValue [] szEmptyArgs = new PropertyValue [0]; PropertyValue [] szEmptyArgs = new PropertyValue [0]; // String doc = "private:factory/swriter"; String doc = "file:///d:/infy.xls"; aDoc = oCLoader.loadComponentFromURL(doc, "_blank", 0, szEmptyArgs ); oDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, aDoc); oCLoader = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); XStorable xt = ( XStorable ) UnoRuntime.queryInterface( XStorable.class, aDoc ); PropertyValue [] mypv = new PropertyValue[1]; mypv[0] = new PropertyValue(); mypv[0].Name = new String("FilterName"); mypv[0].Value = new String("scalc: StarOffice XML (Calc)"); mypv[0].Handle = -1; mypv[0].State = PropertyState.DEFAULT_VALUE; String fl = "file:///d|/Book609.xml"; xt.storeAsURL(fl, mypv); } catch(Exception eq){ //System.out.println("GOT AN ERROR AT "+i +" and "+j); eq.printStackTrace();} return (XSpreadsheetDocument) oDoc; }//end of openCalc public static void main(String[] args){ tst609 t1 = new tst609(); } } // finish class SCalc