示例:使用电子表格文档

From Apache OpenOffice Wiki
Jump to: navigation, search



在此示例中,我们将要求远程服务管理器提供远程 Desktop 对象,并使用其 loadComponentFromURL() 方法创建新的电子表格文档。从文档中我们可以获得其工作表容器,在其中按名称插入和访问新的工作表。在新工作表的 A1 和 A2 中输入值,然后在 A3 中汇总。汇总单 元格的样式获得“结果”单元格样式,因此显示为斜粗体并带下划线。最后,使新工作表成为当前 工作表,这样,用户可以看得到它。


将这些导入行添加到以上 FirstConnection 示例:

  import com.sun.star.beans.PropertyValue;
  import com.sun.star.lang.XComponent;
  import com.sun.star.sheet.XSpreadsheetDocument;
  import com.sun.star.sheet.XSpreadsheets;
  import com.sun.star.sheet.XSpreadsheet;
  import com.sun.star.sheet.XSpreadsheetView;
  import com.sun.star.table.XCell;
  import com.sun.star.frame.XModel;
  import com.sun.star.frame.XController;
  import com.sun.star.frame.XComponentLoader;





编辑 useConnection 方法,如下所示:

  protected void useConnection() throws java.lang.Exception {
        try {
            // get the remote office component context
            xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            System.out.println("Connected to a running office ...");
 
            xRemoteServiceManager = xRemoteContext.getServiceManager();
        }
        catch( Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
 
  try {
        // get the Desktop, we need its XComponentLoader interface to load a new document
        Object desktop = xRemoteServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", xRemoteContext);
 
        // query the XComponentLoader interface from the desktop
        XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
            XComponentLoader.class, desktop);
 
        // create empty array of PropertyValue structs, needed for loadComponentFromURL
        PropertyValue[] loadProps = new PropertyValue[0];
 
        // load new calc file
        XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
            "private:factory/scalc", "_blank", 0, loadProps);
 
        // query its XSpreadsheetDocument interface, we want to use getSheets()
        XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
            XSpreadsheetDocument.class, xSpreadsheetComponent);
 
        // use getSheets to get spreadsheets container
        XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
 
        //insert new sheet at position 0 and get it by name, then query its XSpreadsheet interface
        xSpreadsheets.insertNewByName("MySheet", (short)0);
        Object sheet = xSpreadsheets.getByName("MySheet");
        XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
            XSpreadsheet.class, sheet);
 
        // use XSpreadsheet interface to get the cell A1 at position 0,0 and enter 21 as value
        XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
        xCell.setValue(21);
 
        // enter another value into the cell A2 at position 0,1
        xCell = xSpreadsheet.getCellByPosition(0, 1);
        xCell.setValue(21);
 
        // sum up the two cells
        xCell = xSpreadsheet.getCellByPosition(0, 2);
        xCell.setFormula("=sum(A1:A2)");
 
        // we want to access the cell property CellStyle, so query the cell's XPropertySet interface
        XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
            XPropertySet.class, xCell);
 
        // assign the cell style "Result" to our formula, which is available out of the box
        xCellProps.setPropertyValue("CellStyle", "Result");
 
        // we want to make our new sheet the current sheet, so we need to ask the model
        // for the controller: first query the XModel interface from our spreadsheet component
        XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(
           XModel.class, xSpreadsheetComponent);
 
        // then get the current controller from the model
        XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
 
        // get the XSpreadsheetView interface from the controller, we want to call its method
        // setActiveSheet
        XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)UnoRuntime.queryInterface(
           XSpreadsheetView.class, xSpreadsheetController);
 
        // make our newly inserted sheet the active sheet using setActiveSheet
        xSpreadsheetView.setActiveSheet(xSpreadsheet);     
    }
    catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
        xRemoteContext = null;
        throw e;
    }          
  }

另外,也可以将示例目录中的 FirstLoadComponent.java 添加到当前项目中,它含有上述更改。

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages