简体   繁体   English

使用OpenXML 2.5创建.docx会导致文件损坏

[英]Creating .docx with OpenXML 2.5 causes corrupt file

I am trying to create a document using OpenXML 2.5 SDK as detailed here: 我正在尝试使用OpenXML 2.5 SDK创建文档,如下所示:

https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.document.aspx https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.document.aspx

My code looks identical to this, but when I try to open the created .docx file I get 我的代码看起来与此相同,但是当我尝试打开创建的.docx文件时,我得到了

The file DocumentEx.docx cannot be opened because there are problems with the contents. 无法打开文件DocumentEx.docx,因为内容存在问题。

as an error in Microsoft Word 作为Microsoft Word中的错误

The problem is within the document.xml file which looks something like: 问题在document.xml文件中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <w:document     xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
            <a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                <a:r>
                    <a:t>Hello, Word!</a:t>
                </a:r>
            </a:p>
        </w:body>
    </w:document>

If I change the XML to: 如果我将XML更改为:

<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p>
      <w:r>
        <w:t>Hello, Word!</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:document>

The file will open. 该文件将打开。

How do I get this to not save as corrupted? 如何将此保存为损坏? I must be missing something large to be having such an error from code I pulled from MSDN. 我必须遗漏一些大的东西从我从MSDN中提取的代码中出现这样的错误。

I am developing in Visual Studio 2010, .NET Framework v 4.6 SP1 and attempting to open these .docx files with Microsoft Word 2010. 我正在使用Visual Studio 2010,.NET Framework v 4.6 SP1进行开发,并尝试使用Microsoft Word 2010打开这些.docx文件。

Thank you in advance. 先感谢您。

My problem was that my Namespace for my added objects was incorrect. 我的问题是我的添加对象的命名空间不正确。

I was using DocumentFormat.OpenXML.Drawing for my Text object instead of DocumentFormat.OpenXML.Wordprocessing. 我使用DocumentFormat.OpenXML.Drawing作为Text对象而不是DocumentFormat.OpenXML.Wordprocessing。 This was not rendering the text properly and causing my error. 这不是正确渲染文本并导致我的错误。

This may be for missing many header. 这可能是为了缺少许多标题。 This below code does solves it. 以下代码确实解决了它。 I have used office 2010 我用过办公室2010

 <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
    <w:body>
    <w:p>
    <w:r>
    <w:t>Hello , word</w:t>
    </w:r>
    </w:p>
    <w:sectPr>
    <w:pgSz w:w="12240" w:h="15840"/>
    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
    <w:cols w:space="708"/>
    <w:docGrid w:linePitch="360"/>
    </w:sectPr>
    </w:body>
    </w:document>

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

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