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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/223777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:51:21  来源:igfitidea点击:

How to create .odt files with C#.NET?

c#documentopenoffice.org

提问by ramayac

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 .odtto create file from C# .NET?
Is there a .NET component or wrapper for an OpenOffice.org library to do this?

您知道如何创建.odt以从 C# .NET 创建文件吗?
是否有用于 OpenOffice.org 库的 .NET 组件或包装器来执行此操作?

采纳答案by bmotmans

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)
  • support for text and spreadsheet documents
  • create, read, edit, save documents
  • ...
  • 完全托管的 .NET 1.1(因此它可以在 MS.Net 和 Mono 上运行)
  • 支持文本和电子表格文档
  • 创建、阅读、编辑、保存文档
  • ...

EDIT by kame: New link AODL-Wiki

kame 编辑:新链接 AODL-Wiki

回答by Dillie-O

You can check out the OASIS Standards sitefor 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.

您可以查看OASIS 标准站点以获取有关 ODT 标准的信息。从我看到的情况来看,他们使用基于 XML 的标准,并且有一个 XSD 可用于文档标准,因此您可以将其与您自己的代码结合使用以构建正确格式的文档文件。

回答by VolkerK

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

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

回答by lanoxx

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.

不幸的是,我还没有找到一种无需打开即可创建文件的方法。

回答by CloudyMarble

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();

回答by movAX13h

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. Open Office Specification

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