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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值