简体   繁体   English

OpenXML 错误“文件已损坏,无法打开。” 对于 C# 中的 Word 文档

[英]OpenXML error "file is corrupt and cannot be opened." for a Word Document in C#

I have built a word document with Word 2007 and saved it.我已经用 Word 2007 建立了一个 Word 文档并保存了它。 Then i used OpenXML productivity tool and used code reflection to grab the C# code.然后我使用 OpenXML 生产力工具并使用代码反射来获取 C# 代码。

The next step I did was build a function that returns a tableRow object.我所做的下一步是构建一个 function,它返回一个 tableRow object。 I would use my list I had built with LINQ to populate the text fields of the row.我将使用我用 LINQ 构建的列表来填充行的文本字段。 I copied the row generation code from 1 of the built in rows of the table, and filled in the fields in a loop, using我从表的 1 个内置行中复制了行生成代码,并在循环中填充了字段,使用

foreach(ClassForLinqResult lr in LinqResult)
{
  Table1.Append(CreateRow(lr.param1,lr.param2..etc));
}

The problem remains, when the server responds back with the file, I get an error from word trying to open it:问题仍然存在,当服务器返回文件时,尝试打开它时出现错误:

"The file is corrupt and cannot be opened".

Was there some part of the code I missed that needs to be added somewhere else for the rows I add?是否有一些我错过的代码需要为我添加的行添加到其他地方? As it turns out when I use word to try to recover the file, my rows are showing correctly.事实证明,当我使用 word 尝试恢复文件时,我的行显示正确。

The corrupted file has a package folder with a file in it like this:损坏的文件有一个 package 文件夹,其中有一个文件,如下所示:

package\services\metadata\core-properties\edf684ae4e35438dacc06cb66c0afad2.psmdcp

after it corrects the file it removes that and adds a core.xml file to the "docProps" folder.在更正文件后,它会删除该文件并将 core.xml 文件添加到“docProps”文件夹中。

some other files are changed其他一些文件被更改

customXML\item.xml changes to customXML\item1.xml after recovery
customXML\itemProps.xml changes to customXML\itemProps1.xml after recovery
customXML\item.xml.rels changes to customXML\item1.xml.rels after recovery
word\footer.xml changes to word\footer1.xml after recovery
word\theme\theme.xml changes to word\theme\theme1.xml

Anyway, if anyone has some tools that point out the problem for me I'd appreciate it.无论如何,如果有人有一些工具可以为我指出问题,我将不胜感激。 I have tried我努力了

OpenXmlPackageValidationSettings validationSettings = new OpenXmlPackageValidationSettings();
validationSettings.EventHandler += new EventHandler<OpenXmlPackageValidationEventArgs>(PackageValidationEventHandler);
CreateParts(package,LicenseToPrint);
package.Validate(validationSettings);

This never hits any code indicating there was problem with the validation.这永远不会遇到任何表明验证存在问题的代码。 I have not been able to figure out the problem with the word document.我一直无法弄清楚word文档的问题。 Can anyone help with an idea how to figure out the problem?任何人都可以帮助解决问题吗? Or perhaps I missed a step that is causing my problem?或者我错过了导致我的问题的步骤?

Additional Information:附加信息:

This code is how I am rendering the docx file in asp.net这段代码是我在 asp.net 中渲染 docx 文件的方式

GeneratedClass c = new GeneratedClass();
        using (MemoryStream generatedStream = new MemoryStream())
        {
            c.CreatePackage(generatedStream,LicenseToPrint);
            ValidateDocument(generatedStream);
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; FileName=\"License.docx\"");
            Response.ContentType = "application/msword";
            //Response.ContentType = "application/vnd.ms-word.document";
            generatedStream.WriteTo(Context.Response.OutputStream);
            Context.Response.Flush();
        }

I am not sure what was the cause, but I repeated my steps again and now have a file with no more errors with MS word. 我不确定是什么原因,但是我再次重复了步骤,现在有了一个文件,MS word没有更多错误。 Copying from the Open XML tool into my code screwed up somewhere. 从Open XML工具复制到我的代码中的某个地方搞砸了。 If this happens to anyone doing similar steps, my suggestion is to repeat your steps, it may have been a slip up somewhere doing the copying of the code. 如果执行类似步骤的任何人都遇到这种情况,我的建议是重复您的步骤,这可能是在复制代码的某个地方溜了出来。 The problem is if you have a lot of code written already and there's too many steps to trace back. 问题是,如果您已经编写了很多代码,并且有太多步骤需要追溯。 A versioning system should help in such a case. 在这种情况下,版本控制系统应该会有所帮助。 Thank you all 谢谢你们

I don't have the answer. 我没有答案。 But I can help rule out some of the available information. 但我可以帮助排除一些可用的信息。

customXML\item.xml changes to customXML\item1.xml after recovery
customXML\itemProps.xml changes to customXML\itemProps1.xml after recovery
customXML\item.xml.rels changes to customXML\item1.xml.rels after recovery
word\footer.xml changes to word\footer1.xml after recovery
word\theme\theme.xml changes to word\theme\theme1.xml

Don't worry about the renaming. 不用担心重命名。 Microsoft Word likes to add a suffix-number to any parts that can have multiples. Microsoft Word喜欢在任何可以有多个倍数的部分添加后缀数字。 So when word recovered the document, it recreated the document and used its preferred naming system for those parts. 因此,当word恢复文档时,它会重新创建文档,并对这些部分使用其首选的命名系统。

It's possible the file have not finish writing/saving when you tried to open the file as in my case.当您尝试打开文件时,文件可能尚未完成写入/保存,就像我的情况一样。 I waited a few seconds before opening the file and it worked.我在打开文件之前等了几秒钟,它起作用了。

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

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