updating calc sheet through java in hide mode

Discuss the spreadsheet application

updating calc sheet through java in hide mode

Postby java.qc@gmail.com » Tue Jun 24, 2008 3:04 pm

Hi

I am trying to update my existing calc sheet through java program , but when i try to insert some data into my sheet ,it is first loaded and opened to insert data . My requirement is that my sheet should not be opened while updating it . it is very urgent . Please help me out . Following is the code that I m using to insert some data :


Code: Select all   Expand viewCollapse view
import com.sun.star.beans.PropertyValue;
import com.sun.star.container.XIndexAccess;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.CellContentType;
import com.sun.star.table.XCell;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class ConnectToCalc
{
   
   public static void main(String args[])
   {
       
        XComponentContext xContext = null;
        try
        {
            xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            System.out.println("Connected to a running office ...");
        }
        catch( Exception e)
        {
            e.printStackTrace(System.err);
            System.exit(1);
        }
       
       
        XSpreadsheetDocument myDoc = null;
               
        System.out.println("Opening an empty Calc document........");
        myDoc = openCalc(xContext);
       
      //-----------------------------------Step 3----------------------------------
      // get the sheet an insert some data.
      // Get the sheets from the document and then the first from this container.
      // Now some data can be inserted. For this purpose get a Cell via
      // getCellByPosition and insert into this cell via setValue() (for floats)
      // or setFormula() for formulas and Strings
      //---------------------------------------------------------------------------


      XSpreadsheet xSheet=null;

      try
      {
//          System.out.println("Getting spreadsheet") ;
//          XSpreadsheets xSheets = myDoc.getSheets() ;
//          XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSheets);
//          xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, oIndexSheets.getByIndex(0));
         
      }
      catch (Exception e)
      {
          System.out.println("Couldn't get Sheet " +e);
          e.printStackTrace(System.err);
      }
      System.out.println("Creating the Header") ;
      //insertIntoCell(0,0,"Name",xSheet,"");
      System.out.println("Fill the lines");
      //insertIntoCell(0,19,"Nitin",xSheet,"");
     // System.out.println(getDataFromCell(0,19,xSheet,""));
     
   }//End Of Main Block


   public static XSpreadsheetDocument openCalc(XComponentContext xContext)
   {   
       //define variables
       XMultiComponentFactory xMCF = null;
       XComponentLoader xCLoader;
       XSpreadsheetDocument xSpreadSheetDoc = null;
       XComponent xComp = null;
       XComponent xCom = null;
       try
       {
           // get the service manager from the office
           xMCF = xContext.getServiceManager();
   
           // create a new instance of the the desktop
           Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext );
           // query the desktop object for the XComponentLoader
           xCLoader = ( XComponentLoader ) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop );
           PropertyValue [] szEmptyArgs = new PropertyValue [0];
           //String strDoc = "private:factory/scalc";
           java.io.File sourceFile = new java.io.File("D:\\nitin.ods");
           String strDoc = "file:///"+sourceFile.getCanonicalPath().replace('\\', '/');
          
           java.io.File saveFile =new java.io.File("D:\\nitin.ods");
           //String sSaveUrl="file:///"+saveFile.getCanonicalPath().replace('\\', '/');
           xComp = xCLoader.loadComponentFromURL(strDoc, "_default", 0, szEmptyArgs );
          
          

           xSpreadSheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xComp);
           XSpreadsheet xSheet=null;

           try
           {
               System.out.println("Getting spreadsheet") ;
               XSpreadsheets xSheets = xSpreadSheetDoc.getSheets() ;
               XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSheets);
               xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, oIndexSheets.getByIndex(0));
              
           }
           catch (Exception e)
           {
               System.out.println("Couldn't get Sheet " +e);
               e.printStackTrace(System.err);
           }
           insertIntoCell(2,1,"Message",xSheet,"");
           insertIntoCell(2,19,"Hi",xSheet,"");
           com.sun.star.frame.XStorable xStorable =
                (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
                    com.sun.star.frame.XStorable.class, xComp );
           xStorable.storeAsURL( strDoc.toString(), szEmptyArgs );
           com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
            UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,xComp );

        if (xCloseable != null ) {
            xCloseable.close(false);
        } else
        {
            com.sun.star.lang.XComponent xCompp = (com.sun.star.lang.XComponent)
                UnoRuntime.queryInterface(
                    com.sun.star.lang.XComponent.class, xComp );
            xCompp.dispose();
        }
          
       }
       catch(Exception e)
       {           
           System.err.println(" Exception " + e);
           e.printStackTrace(System.err);
       }       
       return xSpreadSheetDoc;
   }
   
   
   
   
   
   public static void insertIntoCell(int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)
   {   
         XCell xCell = null;
      try
      {
         xCell = TT1.getCellByPosition(CellX, CellY);
      }
      catch (com.sun.star.lang.IndexOutOfBoundsException ex)
      {
         System.err.println("Could not get Cell");
         ex.printStackTrace(System.err);
      }
      if (flag.equals("V"))
      {
         xCell.setValue((new Float(theValue)).floatValue());
      }
      else
      {
         xCell.setFormula(theValue);
      }
   }//End of method insertIntoCell block
      
      
   public static String getDataFromCell(int CellX, int CellY,  XSpreadsheet TT1, String flag)
   {   
      XCell xCell = null;
      String str = null;
      try
      {
         xCell = TT1.getCellByPosition(CellX, CellY);
      }
      catch (com.sun.star.lang.IndexOutOfBoundsException ex)
      {
         System.err.println("Could not get Cell");
         ex.printStackTrace(System.err);
      }
      if (flag.equals("V"))
      {
         Double val=   xCell.getValue();
      }
      else
      {
         str= xCell.getFormula();
         System.out.println(CellContentType.FORMULA_value);
      }
      return str;
   }//End of method getDataFromCell block
}
OOo 2.3.X on Ms Windows W2k
java.qc@gmail.com
 
Posts: 1
Joined: Tue Jun 24, 2008 2:53 pm

Re: updating calc sheet through java in hide mode

Postby Villeroy » Tue Jun 24, 2008 3:20 pm

Added code-tags for readability. There is still no problem description. Do you want someone else to do your work?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28846
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Calc

Who is online

Users browsing this forum: Google [Bot] and 24 guests