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

发布于 2024-10-24 08:56:01 字数 2233 浏览 6 评论 0原文

我用Word 2007建立了一个word文档并保存。然后我使用 OpenXML 生产力工具并使用代码反射来获取 C# 代码。

我所做的下一步是构建一个返回 tableRow 对象的函数。 我将使用我用 LINQ 构建的列表来填充以下文本字段 行。我从 1 个内置行中复制了行生成代码 表,并在循环中填充字段,使用

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

问题仍然存在,当服务器用文件响应时, 我尝试打开它时收到一个错误:

"The file is corrupt and cannot be opened".

是否有我错过的代码的某些部分需要在某处添加 还有我添加的行吗?事实证明,当我使用 word 尝试恢复时 文件中,我的行显示正确。

损坏的文件有一个包文件夹,其中有一个文件,如下所示:

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

更正该文件后,它会删除该文件并将 core.xml 文件添加到 “docProps”文件夹。

其他一些文件已更改

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

无论如何,如果有人有一些工具可以为我指出问题,我将不胜感激。 我已经尝试过了

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

,这从未命中任何表明验证存在问题的代码。 我一直没能弄清楚word文档的问题。 任何人都可以帮忙想出如何解决这个问题吗?或者也许我错过了一步 这导致了我的问题?

附加信息:

此代码是我在 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 have built a word document with Word 2007 and saved it. Then i used OpenXML productivity tool and used code reflection to grab the C# code.

The next step I did was build a function that returns a tableRow object.
I would use my list I had built with LINQ to populate the text fields of
the row. 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

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.

The corrupted file has a package folder with a file in it like this:

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.

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.
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

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();
        }

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

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

发布评论

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

评论(3

贪了杯 2024-10-31 08:56:02

我没有答案。但我可以帮助排除一些可用的信息。

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

不用担心重命名。 Microsoft Word 喜欢为任何可以有多个的部分添加后缀数字。因此,当 Word 恢复文档时,它会重新创建文档并为这些部分使用其首选命名系统。

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. So when word recovered the document, it recreated the document and used its preferred naming system for those parts.

独闯女儿国 2024-10-31 08:56:02

当您尝试打开文件(如我的情况)时,文件可能尚未完成写入/保存。我等了几秒钟才打开文件,它成功了。

另外,请确保按照 Open-XML-SDK 问题中的建议处理使用的流第1188章

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.

Also, make sure to dispose of streams used as recommended in Open-XML-SDK issue#1188

一身仙ぐ女味 2024-10-31 08:56:01

我不确定原因是什么,但我再次重复了我的步骤,现在 MS Word 的文件不再有错误。从 Open XML 工具复制到我的代码中的某个地方搞砸了。如果任何人执行类似的步骤时发生这种情况,我的建议是重复您的步骤,这可能是在复制代码的某个地方出现了失误。问题是,如果您已经编写了很多代码,并且有太多步骤需要追溯。在这种情况下,版本控制系统应该会有所帮助。谢谢大家

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. Copying from the Open XML tool into my code screwed up somewhere. 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

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