简体   繁体   English

如何使用 C#.NET 创建 .odt 文件?

[英]How to create .odt files with C#.NET?

Note: I found this "Creating a Word Doc in C#.NET" , but that is not what I want.注意:我找到了“Creating a Word Doc in C#.NET” ,但这不是我想要的。

Do you know how to create a .odt to create file from C# .NET?你知道如何创建一个.odt来从 C# .NET 创建文件吗?
Is there a .NET component or wrapper for an OpenOffice.org library to do this?是否有用于 OpenOffice.org 库的 .NET 组件或包装器来执行此操作?

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

  • fully managed .NET 1.1 (so it runs on MS.Net and Mono)完全托管的 .NET 1.1(因此它在 MS.Net 和 Mono 上运行)
  • support for text and spreadsheet documents支持文本和电子表格文档
  • create, read, edit, save documents创建、读取、编辑、保存文档
  • ... ...

EDIT by kame: New link AODL-Wiki由 kame 编辑:新链接AODL-Wiki

You can check out the OASIS Standards site for information on the ODT standard.您可以查看OASIS 标准站点以获取有关 ODT 标准的信息。 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.据我所知,他们使用的是基于 XML 的标准,并且有一个可用于文档标准的 XSD,因此您可以将它与您自己的代码结合使用,以正确格式构建文档文件。

您可能对OpenOffice、UNO CLI 语言绑定感兴趣。

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.我昨天在寻找一种创建电子表格的方法时发现了这个,看起来创建编写器文件非常相似: http ://www.suite101.com/content/creating-an-openoffice-calc-document-with-c- a124112 ,别忘了先从 Oracle 安装 Open Office SDK。

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. OpenOffice 文档 (odt) 是 ZIP 文件。 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.您可以解压缩现有的文件,通过代码修改 content.xml 文件,然后使用 System.IO.Compression 中的 ZipFile 类再次获取 zip 文件。 Open Office Specification 开放式办公室规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM