Insert table in Openoffice Swriter(插入一个表格)

     protected XTextTable insertTable(XTextDocument xDoc, XTextCursor xTxCurs, int row, int col)
    {
        try
        {
            XMultiServiceFactory xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xDoc);

            //       Object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xCC);
            // get the remote service manager
            // query its XDesktop interface, we need the current component
            //       XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);

            //       XComponent xWriterComponent = xDesktop.getCurrentComponent();

            //       XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xDoc);

            //       XController xController = xModel.getCurrentController();
            // the controller gives us the TextViewCursor
            //       XTextViewCursorSupplier xViewCursorSupplier =
            //       (XTextViewCursorSupplier)UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
            //       XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
            //get the main text document
            XText mxDocText = xDoc.getText();      

            // Create a new table from the document's factory
            XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(
                    XTextTable.class,
                    xMSF.createInstance(
                            "com.sun.star.text.TextTable" ) );

            // Specify that we want the table to have 4 rows and 4 columns
            xTable.initialize( row    , col );

            XTextRange xPos = xTxCurs.getStart();//xViewCursor.getStart();               
            mxDocText.insertTextContent( xPos, xTable, false);

            // Insert the table into the document
            // Get an XIndexAccess of the table rows

            // Access the property set of the first row (properties listed in service description:
            // com.sun.star.text.TextTableRow)
            //       XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface(
            //           XPropertySet.class, xRows.getByIndex ( 0 ) );
            // If BackTransparant is false, then the background color is visible
            //       xRow.setPropertyValue( "BackTransparent", new Boolean(false));
            // Specify the color of the background to be dark blue
            //       xRow.setPropertyValue( "BackColor", new Integer(6710932));

            // Access the property set of the whole table
                   XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(
                       XPropertySet.class, xTable );

//                   Utilities.showProperties(xTable, xTableProps);
             //set table
            // We want visible background colors
                   xTableProps.setPropertyValue( "RepeatHeadline", new Boolean(true));
                   xTableProps.setPropertyValue( "HeaderRowCount", new Integer(1));
                   xTableProps.setPropertyValue( "HoriOrient", new Short(com.sun.star.text.HoriOrientation.LEFT));
                   xTableProps.setPropertyValue( "RelativeWidth", new Short((short)100));
//WRONG !                   xTableProps.setPropertyValue( "IsWidthRelative", new Boolean(true));

//                   Utilities.showProperties(xTable, xTableProps);
            // Set the background colour to light blue
            //       xTableProps.setPropertyValue( "BackColor", new Integer(13421823));

            // set the text (and text colour) of all the cells in the first row of the table
            //insert some titles
            insertIntoCell( "A1", "Element", xTable );
            insertIntoCell( "B1", "Value", xTable );
            insertIntoCell( "C1", "Notes", xTable );

            /*       // Insert random numbers into the first this three cells of each
       // remaining row
       xTable.getCellByName( "A2" ).setValue( getRandomDouble() );
       xTable.getCellByName( "B2" ).setValue( getRandomDouble() );
       xTable.getCellByName( "C2" ).setValue( getRandomDouble() );

       xTable.getCellByName( "A3" ).setValue( getRandomDouble() );
       xTable.getCellByName( "B3" ).setValue( getRandomDouble() );
       xTable.getCellByName( "C3" ).setValue( getRandomDouble() );

       xTable.getCellByName( "A4" ).setValue( getRandomDouble() );
       xTable.getCellByName( "B4" ).setValue( getRandomDouble() );
       xTable.getCellByName( "C4" ).setValue( getRandomDouble() );*/

            // Set the last cell in each row to be a formula that calculates
            // the sum of the first three cells
            /*        xTable.getCellByName( "D2" ).setFormula( "sum <A2:C2>" );
       xTable.getCellByName( "D3" ).setFormula( "sum <A3:C3>" );
       xTable.getCellByName( "D4" ).setFormula( "sum <A4:C4>" );*/
            return xTable;
        }
        catch (Exception e)
        {
            e.printStackTrace ( System.out );
        }
        return null;
    }

你可能感兴趣的:(Insert table in Openoffice Swriter(插入一个表格))