OpenOffice Java code format table cells

Java, C++, C#, Delphi, ??? - Using the UNO bridges

OpenOffice Java code format table cells

Postby sarthakjain99.007 » Thu Aug 24, 2017 6:19 am

I am creating a table using openoffice in Java and want to format the table cell contents like giving background color, change font style, aligning etc.
My code is

Code: Select all   Expand viewCollapse view
XMultiServiceFactory xMSF = ( XMultiServiceFactory ) UnoRuntime.queryInterface(XMultiServiceFactory.class, xdocument);

       // Creating a table with 2 rows and 2 columns
       XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface(XTextTable.class, xMSF.createInstance( "" ) );
       xTextTable.initialize( 2, 2); // rows, cols

       // insert table  in the xText

       xText.insertTextContent(xText.getEnd(), xTextTable, false);
       XPropertySet xPS1 = ( XPropertySet ) UnoRuntime.queryInterface(
       XPropertySet.class, xTextTable );

       // Get table Width and TableColumnRelativeSum properties values
       int iWidth = ( Integer ) xPS1.getPropertyValue( "Width" );
       short sTableColumnRelativeSum = ( Short ) xPS1.getPropertyValue( "TableColumnRelativeSum" );

       // Calculate conversion ration
        double dRatio = ( double ) sTableColumnRelativeSum / ( double ) iWidth;

        // Convert our 20 mm (2000) to unknown ( relative ) units
       double dRelativeWidth = ( double ) 25000 * dRatio;

       // Get table column separators
       Object xObj = xPS1.getPropertyValue( "TableColumnSeparators" );

       TableColumnSeparator[] xSeparators = ( TableColumnSeparator[] )UnoRuntime.queryInterface(
       TableColumnSeparator[].class, xObj );

       // Last table column separator position
       double dPosition = sTableColumnRelativeSum - dRelativeWidth;

       // Set set new position for all column separators       
       for ( int i = xSeparators.length - 1 ; i >= 0 ; i-- )
       xSeparators[i].Position = (short) Math.ceil( dPosition );
        dPosition -= dRelativeWidth;

       // Do not forget to set TableColumnSeparators back! Otherwise, it doesn't work.
       xPS1.setPropertyValue( "TableColumnSeparators", xSeparators );

       XCellRange xCellRangeHeader = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, xTextTable);
       XCell xCellHeader = null;
       XText xHeaderText = null;

       xCellHeader = xCellRangeHeader.getCellByPosition(0,0); // cols, rows
       xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
       xHeaderText.setString("Records Center Total Capacity");

       xCellHeader = xCellRangeHeader.getCellByPosition(1,0); // cols, rows
       xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);

       xCellHeader = xCellRangeHeader.getCellByPosition(0,1); // cols, rows
       xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
       xHeaderText.setString("Current Inventory For Week Ending");

       xCellHeader = xCellRangeHeader.getCellByPosition(1,1); // cols, rows
       xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);

Please help me in doing that.
Open Office 4, Windows 7
Posts: 3
Joined: Thu Aug 24, 2017 6:11 am

Re: Openoffice Java code format table cells

Postby Villeroy » Thu Aug 24, 2017 4:51 pm

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, OpenOffice 4.x & LibreOffice 6.x
User avatar
Posts: 26264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OpenOffice Java code format table cells

Postby janfl » Thu Oct 12, 2017 8:44 pm


Regarding Table Column Width and Java there is a bug:

That being said there are other options:

1.Create a table with correct setting in Calc and copy/paste that to writer.
2.Create a template and make a conditional formatting in a macro. You can store that macro to your template. You can execute your macro from java.

In case of the column width it will not help, but sometimes I first create a working Macro in Open Office it self and then in myu own program language.
In OO you can inspect the objects and variables.

Regarding MRI I can mention Xray too. These extensions let you see wich methods an propertys there are, you can see the actual setting and the online documenteation.
A third option is inspector. I work with Xray. Nice from MRI is that you can see example program code.
I agree when you say that this are not ideal options, but that's all I know.

Kind regards,

Jan Flikweert
Open Office 4.1.3 Windows 10
Posts: 29
Joined: Thu Sep 28, 2017 4:17 pm

Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest