C#: Creating Tables in Open Office using C#.... Open Office Sucks and So Does Your Face

OK ... you're going to have to use your imagination a bit here because there is too much code to put everything in here ... the basic premise is as such:

// get the row counts out of my parameter => MyUserDefinedCustomTableObject is obviously a class in my app - and no, it is not a DataTable it is a Generic Matrix.
int RowCount = MyUserDefinedCustomTableObject.Rows.Count;

// get the col counts out of ma parameter
int ColCount = MyUserDefinedCustomTableObject.Rows[0].Cells.Count;

// create a context consisting of the oo bootstrap
unoidl.com.sun.star.uno.XComponentContext localContext = uno.util.Bootstrap.bootstrap();

// create a service factory to get shit from
unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory )localContext.getServiceManager();

// create a component loader to get stuff from
XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance( "com.sun.star.frame.Desktop");

// create a frame which is basically the document reference
XFrame frame = ((unoidl.com.sun.star.text.XTextDocument)xComponent).getCurrentController().getFrame();

// the tricky part, create a dispatch helper - this is the work horse
XDispatchHelper xDispatchHelper = (XDispatchHelper )multiServiceFactory.createInstance( "com.sun.star.frame.DispatchHelper");

// this is only used if you want to set bookmark text within the table cells ... we use bookmarks as placeholders where there will be text in the future.
XNameAccess xna = ((XBookmarksSupplier)xComponent).getBookmarks();

// Create the property values - this uses a local function that I'm not nice enough to give you but on a primitive level it is propertyvalue.Name = first param, propertyvalue.value = 2nd param.
PropertyValue[] tableArgs = new unoidl.com.sun.star.beans.PropertyValue[4];
tableArgs[0] = CreateNewProperty("TableName", new uno.Any(MyUserDefinedCustomTableObject.TableName));
tableArgs[1] = CreateNewProperty("Columns", new uno.Any(ColCount));
tableArgs[2] = CreateNewProperty("Rows", new uno.Any(RowCount));

// check to determine if we should show borders
if (MyUserDefinedCustomTableObject.ShowBorders)
tableArgs[3] = CreateNewProperty("Flags", new uno.Any(9));
tableArgs[3] = CreateNewProperty("Flags", new uno.Any(8));

// dispatch the table creation event to the UI ...
xDispatchHelper.executeDispatch((XDispatchProvider)frame, ".uno:InsertTable", "", 0, tableArgs);

// you have a table in your document, the cursor is usually in the first cell of the table so you can use this function to jump between cells and insert text / bookmarks.
xDispatchHelper.executeDispatch((XDispatchProvider)frame, ".uno:JumpToNextCell", "", 0, new unoidl.com.sun.star.beans.PropertyValue[0]);

// if you are in a table cell and want to set the text of that cell then you can use this
cellTextArgs[0] = CreateNewProperty("Text", new uno.Any("My lovely text i love you text"));
xDispatchHelper.executeDispatch((XDispatchProvider)frame, ".uno:InsertText", "", 0, cellTextArgs);

// if you want to insert a bookmark into the cell you could try this (note you do not have to be in a table cell for this to insert a bookmark).
PropertyValue[] cellBkmkArgs = new unoidl.com.sun.star.beans.PropertyValue[1];
cellBkmkArgs[0] = CreateNewProperty("Bookmark", new uno.Any(cell.CellBookmark));
xDispatchHelper.executeDispatch((XDispatchProvider)frame, ".uno:InsertBookmark", "", 0, cellBkmkArgs);

Over and Out


Awadhesh Singh said...

Please Provide the Source Code of OPen Office Aotumation
Mostly Creating Writer (Table, Inserting PIcture etc.)


Matt Stark said...

I wanted to state the above is not the best way to do this. If people want me to help with Open Office automation in C# please leave a comment with your email on this board and we can chat! Cheers, Matt