如何创建.NET OpenOffice文档 [英] How to create an OpenOffice document in .NET

查看:118
本文介绍了如何创建.NET OpenOffice文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始编写一个应用程序,将输出的OpenOffice文档。在一个空的ODT文档的content.xml文件的开头:

I am starting to write an application that will output an OpenOffice document. The content.xml file in an empty ODT document begins:

<?xml version="1.0" encoding="UTF-8"?>
<office:document-content
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
    xmlns:math="http://www.w3.org/1998/Math/MathML"
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
    xmlns:ooo="http://openoffice.org/2004/office"
    xmlns:ooow="http://openoffice.org/2004/writer"
    xmlns:oooc="http://openoffice.org/2004/calc"
    xmlns:dom="http://www.w3.org/2001/xml-events"
    xmlns:xforms="http://www.w3.org/2002/xforms"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rpt="http://openoffice.org/2005/report"
    xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
    xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#"
    xmlns:
        field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0"
    office:version="1.2">

我还没有看到XML冒号之前。据我所知,他们表示命名空间。我该如何去有关.NET中重现呢?

I haven't seen colons in XML before. I understand that they signify namespaces. How do I go about recreating this in .NET?

推荐答案

以下code将加载所有适当的命名空间到一个命名空间管理和创建示例文件。 .NET只会输出在文档中实际使用虽然命名空间,不是所有的人。我不知道是否有一种方法来强制的未使用的输出,但是未使用的确实不应该需要。

The following code will load up all of the proper namespaces into a namespace manager and create a sample document. .NET will only output the namespaces that are actually used in the document though, not all of them. I'm not sure if there is a way to force output of the unused ones, but unused ones really shouldn't be needed.

public static void Test()
{
    var nt = new NameTable();
    var ns = new XmlNamespaceManager(nt);
    var doc = new XmlDocument(nt);

    ns.AddNamespace("office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0");
    ns.AddNamespace("style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0");
    ns.AddNamespace("text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0");
    ns.AddNamespace("table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0");
    ns.AddNamespace("draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0");
    ns.AddNamespace("fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0");
    ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
    ns.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
    ns.AddNamespace("meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
    ns.AddNamespace("number", "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0");
    ns.AddNamespace("svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
    ns.AddNamespace("chart", "urn:oasis:names:tc:opendocument:xmlns:chart:1.0");
    ns.AddNamespace("dr3d", "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0");
    ns.AddNamespace("math", "http://www.w3.org/1998/Math/MathML");
    ns.AddNamespace("form", "urn:oasis:names:tc:opendocument:xmlns:form:1.0");
    ns.AddNamespace("script", "urn:oasis:names:tc:opendocument:xmlns:script:1.0");
    ns.AddNamespace("ooo", "http://openoffice.org/2004/office");
    ns.AddNamespace("ooow", "http://openoffice.org/2004/writer");
    ns.AddNamespace("oooc", "http://openoffice.org/2004/calc");
    ns.AddNamespace("dom", "http://www.w3.org/2001/xml-events");
    ns.AddNamespace("xforms", "http://www.w3.org/2002/xforms");
    ns.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
    ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    ns.AddNamespace("rpt", "http://openoffice.org/2005/report");
    ns.AddNamespace("of", "urn:oasis:names:tc:opendocument:xmlns:of:1.2");
    ns.AddNamespace("rdfa", "http://docs.oasis-open.org/opendocument/meta/rdfa#");
    ns.AddNamespace("field", "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0");

    var root = doc.CreateElement("office", "document-content");
    var attr = doc.CreateAttribute("office", "version", ns.LookupNamespace("office"));
    attr.Value = "1.2";
    root.Attributes.Append(attr);
    doc.AppendChild(root);

    using (var xw = new XmlTextWriter(Console.Out))
    {
        xw.Formatting = Formatting.Indented;
        xw.Indentation = 2;
        xw.IndentChar = ' ';

        doc.WriteTo(xw);
    }
}

这篇关于如何创建.NET OpenOffice文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆