`WordprocessingDocument.Open`后损坏的文件

时间:2016-01-30 09:50:20

标签: c# openxml

我有这个问题:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { }

在上面使用并在Word中尝试打开文档,显示文件已损坏的错误消息。有趣的是,对于LibreOffice文件没问题。我在使用此代码之前和之后比较了WinMarge文件中的xml文件(在docx中),两者都是相同的。差异仅在于docx文件的大小 - 为什么?

2 个答案:

答案 0 :(得分:1)

好的..我解决了问题..这不是一个很好的解决方案,但它有效..

var document = "template.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
    // some editing stuff
    wordDoc.Clone("ready.docx");
}

现在template.docx已损坏,但ready.docx没问题。

答案 1 :(得分:0)

我有同样的问题,虽然你的解决方案适合我

  

wordDoc.Clone(" ready.docx&#34);

我发现在我的情况下,这是字母编码的问题。我有从abby生成的文件,图像文本到docx生成。 为了检查编码是否会让您遇到麻烦:

  1. 将youreditedword。 docx 更改为youreditedword。 zip
  2. 打开.zip,转到word文件夹,打开document.xml
  3. 检查document.xml'出现在单词中的文本。如果它是乱码,那么你有编码问题。
  4. 我用这种方式修复了 - 删除了原始未编辑的word文件中的一个短语并再次将其写下来并保存。这种方式可能会改变文件的编码方式。然后使用openxml库并打开文件后没有生成文件已损坏错误

相关问题