From Apache OpenOffice Wiki
< Writer
Revision as of 10:32, 11 November 2009 by B michaelsen (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories:

Internal Documentation:

API Documentation:

Ongoing Efforts:


Inserting Tables

To insert a table by supposing opened the Writer document, with a cursor of writing positioned where you want to insert the table, here how to insert a table of 4 lines and 4 columns with com.sun.star.text.XTextTable interface:

//Listing 28 Inserting a Table in a OOoWriter Document
// C++
// Jump to the end of the text
// Create a new text table from the document's factory
// Don't forget to add : #include <com/sun/star/text/XTextTable.hpp>
// Don't forget to add "com.sun.star.text.XTextTable \" in the makefile
// getting MSF of the document
   Reference<XMultiServiceFactory> oDocMSF (xTextDocument,UNO_QUERY);
   Reference <XTextTable> xTable (oDocMSF->createInstance(
   if (!xTable.is()) {
	printf("Erreur creation XTextTable interface !\n");
	return 1;
// Specify that we want the table to have 4 rows and 4 columns
   xTable->initialize(4, 4);
   xTextRange = xText->getEnd();
// Don't forget to add : #include <com/sun/star/text/XTextContent.hpp>
// Don't forget to add "com.sun.star.text.XTextContent \" in the makefile
   Reference <XTextContent>xTextContent (xTable,UNO_QUERY);
// Insert the table into the document
   xText->insertTextContent(xTextRange, xTextContent,(unsigned char) 0);

Here you can see an empty table in your document as shown below :

As seen, the com.sun.star.text.XTextTable interface is our friend. Let's see what this interface provide through the corresponding IDL file :

//Listing 29 The XTextTable Service
// IDL
module com {  module sun {  module star {  module text { 
interface XTextTable: com::sun::star::text::XTextContent
	void initialize( [in] long nRows, 
			 [in] long nColumns ); 
	com::sun::star::table::XTableRows getRows(); 
	com::sun::star::table::XTableColumns getColumns(); 
	com::sun::star::table::XCell getCellByName( [in] string aCellName ); 
	sequence<string> getCellNames(); 
	com::sun::star::text::XTextTableCursor createCursorByCellName( [in] string aCellName );  
}; }; }; };

When reading this listing we see at means two other interesting interfaces com.sun.star.table.XCell and com.sun.star.text.XTextTableCursor.

//Listing 30 The XTextTableCursor Service
// IDL
module com {  module sun {  module star {  module text { 
interface XTextTableCursor: com::sun::star::uno::XInterface
	string getRangeName(); 
	boolean gotoCellByName( [in] string aCellName, 
			 [in] boolean bExpand ); 
	boolean goLeft( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goRight( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goUp( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goDown( [in] short nCount, 
			 [in] boolean bExpand ); 
	void gotoStart( [in] boolean bExpand ); 
	void gotoEnd( [in] boolean bExpand ); 
	boolean mergeRange(); 
	boolean splitRange( [in] short nCount, 
			 [in] boolean bHorizontal );  
}; }; }; };

Navigating the Table

We see now how to navigate through the table. In our example below we shall use the getCellByName to reach a particular cell and obtain the corresponding com.sun.star.table.XCell interface.

//Listing 31 The XCell Service
// IDL
module com {  module sun {  module star {  module table {
interface XCell: com::sun::star::uno::XInterface
	string getFormula();
	void setFormula( [in] string aFormula );
	double getValue();
	void setValue( [in] double nValue );
	com::sun::star::table::CellContentType getType();
	long getError();
}; }; }; };

Changing Appearance

We give now a program to change the background colors in the table and to write something in the cells. It is necessary to add Listing 28 code.

//Listing 32 Changing Colors and writing in the Cells
// C++
// Get the property set of the text table
// Don't forget to add : using namespace com::sun::star::beans;
// Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp>
// Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile
   Reference<XPropertySet> xTableProps (xTable,UNO_QUERY);
   Any prop;
   prop <<= (sal_Bool)false;
   prop <<= (long)0x0000FF; // color blue
// I want to change the first row color
// Don't forget to add : using namespace com::sun::star::table;
// Don't forget to add : #include <com/sun/star/table/XTableRows.hpp>
// Don't forget to add "com.sun.star.table.XTableRows \" in the makefile
   Reference<XTableRows> xTableRows = xTable->getRows();
   Reference<XIndexAccess> theRows (xTableRows,UNO_QUERY);
   Reference<XPropertySet> xRowProps (theRows->getByIndex((short)0),UNO_QUERY);
   prop <<= (sal_Bool)false;
   prop <<= (long)0x000099;
// it's time to add some text now
   Reference<XCell> xCell = xTable->getCellByName(OUString::createFromAscii("A1"));
   xText = Reference<XText>(xCell,UNO_QUERY);
   xTextCursor = xText->createTextCursor();
   xCell = xTable->getCellByName(OUString::createFromAscii("A2"));
   xCell = xTable->getCellByName(OUString::createFromAscii("A3"));
   xCell = xTable->getCellByName(OUString::createFromAscii("A4"));
   xCell = xTable->getCellByName(OUString::createFromAscii("B1"));
   xText = Reference<XText>(xCell,UNO_QUERY);
   xTextCursor = xText->createTextCursor();
   Reference<XPropertySet> oCPS(xTextCursor,UNO_QUERY);
   prop <<= (long)0xFF0000; // color red  
   xCell = xTable->getCellByName(OUString::createFromAscii("B2"));
   xCell = xTable->getCellByName(OUString::createFromAscii("B3"));
   xCell = xTable->getCellByName(OUString::createFromAscii("B4"));

The table above shows us the result obtained with such a code :









See also com.sun.star.beans.XPropertySet and com.sun.star.table.XTableRows.

We want now to tackle few page properties. This is done starting with a Java program. Go back to Programming OOoWriter

Personal tools