OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Java code to convert *.sxc file to *.xls

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
sanvaan
General User
General User


Joined: 05 Jan 2012
Posts: 13
Location: Bangalore

PostPosted: Thu Jan 05, 2012 2:55 am    Post subject: Java code to convert *.sxc file to *.xls Reply with quote

Hello,

Presently i am facing a very tough task in converting a *.sxc file to a *.xls file. I have a piece of code which does the conversion but stores the target as a empty excel file. Can anyone please let me know whats wrong with the code or if any alternate measures can be taken to resolve the problem. The code is as follows.
Code:

private void convertSXCtoXLS() throws Exception {
      
      XComponentContext initialContext = Bootstrap.createInitialComponentContext(null);
       Object connectorInstance = initialContext.getServiceManager().createInstanceWithContext("com.sun.star.connection.Connector", initialContext);
       Object bridgeInstance = initialContext.getServiceManager().createInstanceWithContext("com.sun.star.bridge.BridgeFactory", initialContext);
        XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, connectorInstance);
        XConnection connection = connector.connect("socket,host=127.0.0.1,port=8100");
       
       
        XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, bridgeInstance);
        XBridge bridge = bridgeFactory.createBridge("", "urp", connection, null);

        // Retrieve the Service Manager
        bridgeInstance = bridge.getInstance("StarOffice.ServiceManager");
        XMultiComponentFactory multiComponentFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridgeInstance);

        // Get access to Desktop obj.
        XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
              multiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", initialContext));
       
        XComponentLoader desktop = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);

        PropertyValue[] loadProps = new PropertyValue[2];
      loadProps[0] = new PropertyValue();
      loadProps[0].Name = "Hidden";
      loadProps[0].Value = new Boolean(true);
      
      sourceSXC_URL = property.getProperty("SOURCE_SXC_LOC");
      
      XComponent calcDoc_XComponent = desktop.loadComponentFromURL( sourceSXC_URL, "_blank", 0, loadProps );
       
      XModel model = (XModel)UnoRuntime.queryInterface(XModel.class, calcDoc_XComponent);
      if (model != null) {
         System.out.println("File URL: "+model.getURL());
         
         XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, model);
         
         
         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, xSpreadsheetDocument);
         
      
         PropertyValue[] props = new PropertyValue[1];
         props[0] = new PropertyValue();
         props[0].Name = "FilterName";
         props[0].Value = "MS Excel 97";
         
         targetExcel_URL = property.getProperty("TARGET_XLS_LOC");
         //targetExcel_URL="file:///C:/Documents and Settings/a498863/Desktop/final_lucent.xls";
         
         xStorable.storeToURL(targetExcel_URL, props);
         calcDoc_XComponent.dispose();
         System.exit(0);
      }
      
   }




Moderation probe1: moved to MACROS AND API section, where all macro related questions belong to
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Thu Jan 05, 2012 12:25 pm    Post subject: Reply with quote

I gave your Java source code a try and had no real problems converting a *.sxc to a *.xls.

I tried the example on Ubuntu 10.04 using Sun JDK 1.6.0, OOo 3.2.0 and eclipse.

To get a working connection I used the bootstrapconnector.jar from http://user.services.openoffice.org/en/forum/viewtopic.php?f=44&t=2520&hilit=bootstrapconnector.

I took your Java source code and created a working class:
Code:
package ooo.calc;

import ooo.connector.BootstrapSocketConnector;

import com.sun.star.beans.PropertyValue;
import com.sun.star.bridge.XBridge;
import com.sun.star.bridge.XBridgeFactory;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.connection.XConnection;
import com.sun.star.connection.XConnector;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class ConvertToXls {

   private static final String OOO_PROGRAM_FOLDER = "/usr/lib/openoffice/program";
   
   private void convertSXCtoXLS() throws Exception {
   
/*      
      XComponentContext initialContext = Bootstrap.createInitialComponentContext(null);
      Object connectorInstance = initialContext.getServiceManager().createInstanceWithContext("com.sun.star.connection.Connector", initialContext);
      Object bridgeInstance = initialContext.getServiceManager().createInstanceWithContext("com.sun.star.bridge.BridgeFactory", initialContext);
      XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, connectorInstance);
      XConnection connection = connector.connect("socket,host=127.0.0.1,port=8100");
      
      XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, bridgeInstance);
      XBridge bridge = bridgeFactory.createBridge("", "urp", connection, null);
      
      // Retrieve the Service Manager
      bridgeInstance = bridge.getInstance("StarOffice.ServiceManager");
      XMultiComponentFactory multiComponentFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridgeInstance);
*/

      // Connect to OOo
      XComponentContext remoteContext = BootstrapSocketConnector.bootstrap(OOO_PROGRAM_FOLDER);
      XMultiComponentFactory multiComponentFactory = remoteContext.getServiceManager();

      // Get access to Desktop obj.
      XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, multiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", remoteContext));
      XComponentLoader desktop = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);

      PropertyValue[] loadProps = new PropertyValue[2];
      loadProps[0] = new PropertyValue();
      loadProps[0].Name = "Hidden";
      loadProps[0].Value = new Boolean(true);

      String sourceSXC_URL = "file:///home/joe/Desktop/source.sxc";
      XComponent calcDoc_XComponent = desktop.loadComponentFromURL( sourceSXC_URL, "_blank", 0, loadProps );
         
      XModel model = (XModel)UnoRuntime.queryInterface(XModel.class, calcDoc_XComponent);
      if (model != null) {
         System.out.println("File URL: "+model.getURL());
   
         XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, model);
         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, xSpreadsheetDocument);
   
         PropertyValue[] props = new PropertyValue[1];
         props[0] = new PropertyValue();
         props[0].Name = "FilterName";
         props[0].Value = "MS Excel 97";
   
         String targetExcel_URL = "file:///home/joe/Desktop/target.xls";
   
         xStorable.storeToURL(targetExcel_URL, props);
         calcDoc_XComponent.dispose();
      }
   }

   public static void main(String[] args) {
      ConvertToXls converter = new ConvertToXls();
      try {
         converter.convertSXCtoXLS();
      }
      catch (Exception ex) {
         ex.printStackTrace();
      }
      System.exit(0);
   }
}

This code converted my *.sxc file without a problem to a *.ods file.

First, I thought, the line "PropertyValue[] loadProps = new PropertyValue[2];" might cause a problem, because you used only one of the properties, but even that line did not cause a problem.
Back to top
View user's profile Send private message
sanvaan
General User
General User


Joined: 05 Jan 2012
Posts: 13
Location: Bangalore

PostPosted: Fri Jan 06, 2012 12:58 am    Post subject: Java code to convert *.sxc file to *.xls Reply with quote

I tried using the code you gave and i end up getting the following error.

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at lucentMonthlyRepGen.ConvertToXls.convertSXCtoXLS(ConvertToXls.java:3Cool
at lucentMonthlyRepGen.ConvertToXls.main(ConvertToXls.java:75)

I am presently using OpenOffice 3.3, java 1.5 and eclipse. Is there any different version of BootStrapConnector.jar for java 1.5 or is something wrong with my configuration.
Back to top
View user's profile Send private message
sanvaan
General User
General User


Joined: 05 Jan 2012
Posts: 13
Location: Bangalore

PostPosted: Fri Jan 06, 2012 4:44 am    Post subject: Reply with quote

I copied the source file from the jar onto my eclipse and it seemed to obtain the connection but returns me null value for calcDoc_XComponent in the following line.

Code:

XComponent calcDoc_XComponent = desktop.loadComponentFromURL( sourceSXC_URL, "_blank", 0, loadProps );


From there the program exits and the save does not happen.
What could be the issue.

Thanks in Advance..
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Jan 06, 2012 8:12 am    Post subject: Re: Java code to convert *.sxc file to *.xls Reply with quote

sanvaan wrote:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
...

I am presently using OpenOffice 3.3, java 1.5 and eclipse. Is there any different version of BootStrapConnector.jar for java 1.5 or is something wrong with my configuration.

There is no other version available of the JAR, but to get around that errors I put the source code in the JAR file that you obviously already found, as far as I understood your other post.
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Jan 06, 2012 8:16 am    Post subject: Reply with quote

sanvaan wrote:
I copied the source file from the jar onto my eclipse and it seemed to obtain the connection but returns me null value for calcDoc_XComponent in the following line.

Code:

XComponent calcDoc_XComponent = desktop.loadComponentFromURL( sourceSXC_URL, "_blank", 0, loadProps );


From there the program exits and the save does not happen.
What could be the issue.

All I could imagine is:
- you initialized the BoostrapConnector with the wrong path
or
- (more likely) that yor sourceSXC_URL is wrong
or
- your Java version does not work correctly. Are you using the Oracle (former SUN) JDK?
Back to top
View user's profile Send private message
sanvaan
General User
General User


Joined: 05 Jan 2012
Posts: 13
Location: Bangalore

PostPosted: Mon Jan 09, 2012 2:02 am    Post subject: Java code to convert *.sxc file to *.xls Reply with quote

I have the registered version of java 1.5 from SUN. (Will the code work fine with 1.5 or is 1.6 needed?)

The sourceSXC_URL is correct and points to a folder containing the *.sxc file.

BoostrapConnector points to the open office installation folder which in my case is C:/Program Files/OpenOffice.org 3/program/

Though all are correct the loadComponentFromURL returns me a null value. Does any PropertyValue called "FilterName" needs to be provided for the loadComponentFromURL's property?
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Mon Jan 09, 2012 11:52 am    Post subject: Re: Java code to convert *.sxc file to *.xls Reply with quote

sanvaan wrote:
Will the code work fine with 1.5 or is 1.6 needed?

I tried it with 1.6.0 although I think it works equally good with 1.5.x.

sanvaan wrote:
The sourceSXC_URL is correct and points to a folder containing the *.sxc file.

You assume that it is correct. What does it look like? Is it possible that there is an access right problem of some sort?

sanvaan wrote:
Though all are correct

Most obviously not in your case, or you wouldn't still be searching for a solution Wink

sanvaan wrote:
Does any PropertyValue called "FilterName" needs to be provided for the loadComponentFromURL's property?

Definitly not. loadComponentFromURL does not need anything like that, see my example above.
Back to top
View user's profile Send private message
sanvaan
General User
General User


Joined: 05 Jan 2012
Posts: 13
Location: Bangalore

PostPosted: Tue Jan 24, 2012 4:55 am    Post subject: Reply with quote

Hello,

The sxc file i try to open is actually not in good state. when i try opening the file using open Office tool it tell that it needs to be repaired and from the code i am able to access the repaired document.

So now the problem lies in the sxc file that i am trying to save as xls. This brings me to a new problem.

Is there a way i can automatically repair and open a document from the same java code. Are there any API's to handle such scenarios.

The sxc file is generated by a upstream application on which i do not have hold on and the sxc file always needs to be repaired while opening. Please let me know a work around for this problem.
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Tue Jan 24, 2012 10:24 am    Post subject: Reply with quote

sanvaan wrote:
Is there a way i can automatically repair and open a document from the same java code. Are there any API's to handle such scenarios.

I never tried anything like that.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group