Question

Note: I found this "Creating a Word Doc in C#.NET", but that is not what I want.

Do you know how to create a .odt to create file from C# .NET?
Is there a .NET component or wrapper for an OpenOffice.org library to do this?

Was it helpful?

Solution

Have a look at AODL (see http://odftoolkit.org/projects/odftoolkit/pages/AODL).

  • fully managed .NET 1.1 (so it runs on MS.Net and Mono)
  • support for text and spreadsheet documents
  • create, read, edit, save documents
  • ...

EDIT by kame: New link AODL-Wiki

OTHER TIPS

You can check out the OASIS Standards site for information on the ODT standard. From what I've seen, they're using an XML based standard and have an XSD available for the the document standard, so you could use that in conjunction with your own code to build a document file in the proper format.

You might be interested in OpenOffice, UNO CLI Language Binding.

I found this one yesterday when looking for a way to create Spreadsheeets, it looks like creating writer files is quite similar: http://www.suite101.com/content/creating-an-openoffice-calc-document-with-c-a124112, dont forget to install the Open Office SDK from Oracle first.

Unfortunately I have not found a way to create a file without opening it yet.

The code would look like this:

private XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory oServMan = (XmultiServiceFactory) oStrap.getServiceManager();
XComponentLoader oDesk = (XComponentLoader) oServMan.createInstance("com.sun.star.frame.Desktop");
string url = @"private:factory/swriter";
PropertyValue[] propVals = new PropertyValue[0];
XComponent oDoc = oDesk.loadComponentFromURL(url, "_blank", 0, propVals);
string docText = "File Content\n\r";
((XTextDocument)oDoc).getText().setString(docText);
string fileName = @"C:\FolderName\FileName.odt";
fileName = "file:///" + fileName.Replace(@"\", "/");
((XStorable)oDoc).storeAsURL(fileName, propVals);
((Xcomponent)oDoc).dispose();

OpenOffice documents (odt) are ZIP files. You can unzip an existing one, modify the content.xml file by code and then use the ZipFile class from System.IO.Compression for example to get a zip file again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top