提供使用 Open XML 从 asp.net 网页生成的 Word 文档

发布于 2024-11-19 15:51:12 字数 1618 浏览 2 评论 0原文

我正在尝试使用以下代码从 asp.net 页面提供 Word 文档 (docx),

    protected void Page_Load(object sender, EventArgs e)
    {

        MemoryStream document = new MemoryStream();
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
            SetMainDocumentContent(mainPart);
        }

        string fileName = "MsWordSample.docx";
        Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
        Response.OutputStream.Write(document.ToArray(), 0, document.ToArray().Length);
    }

    public static void SetMainDocumentContent(MainDocumentPart part)
    {
        const string docXml =
         @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
        <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
        <w:body>
            <w:p>
                <w:r>
                    <w:t>Hello world!</w:t>
                </w:r>
            </w:p>
        </w:body>
        </w:document>";

        using (Stream stream = part.GetStream())
        {
            byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
            stream.Write(buf, 0, buf.Length);
        }
    }

但在尝试打开该文件时收到以下消息:“无法打开文件 {文件名},因为存在问题内容”

然后我会得到一个恢复文档的选项,这实际上修复了文档。

我遗漏了什么吗?

I'm trying to serve a word document (docx) from an asp.net page using the following code

    protected void Page_Load(object sender, EventArgs e)
    {

        MemoryStream document = new MemoryStream();
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
            SetMainDocumentContent(mainPart);
        }

        string fileName = "MsWordSample.docx";
        Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
        Response.OutputStream.Write(document.ToArray(), 0, document.ToArray().Length);
    }

    public static void SetMainDocumentContent(MainDocumentPart part)
    {
        const string docXml =
         @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
        <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
        <w:body>
            <w:p>
                <w:r>
                    <w:t>Hello world!</w:t>
                </w:r>
            </w:p>
        </w:body>
        </w:document>";

        using (Stream stream = part.GetStream())
        {
            byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
            stream.Write(buf, 0, buf.Length);
        }
    }

but I get the following message when trying to open the file: "the file {file name} cannot be opened because there are problems with the content"

I then get an option to recover the document which actually fixes the document.

Am I leaving something out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-11-26 15:51:12

您收到的错误消息意味着底层 XML 在某种程度上不符合架构。这可能是由于元素顺序错误、忘记元素的父元素、添加错误的属性、忘记必需的元素等引起的。我建议下载 打开 XML SDK 2.0 Productivity Tool,创建一个测试文件,然后打开该文件以查看 XML 应该是什么。然后将该文件与您正在创建的文件进行比较,看看缺少什么。

That error message you're getting means the underlying XML doesn't conform to the schema in some way. This could be caused by having elements in the wrong order, forgetting a parent to an element, adding a wrong attribute, forgetting required elements, etc. I recommend downloading the Open XML SDK 2.0 Productivity Tool, create a test file, and then opening that up to see what the XML should be. Then compare that file to the one you are creating to see what is missing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文