c# - C# 中的 Word 文档的 OpenXML 错误 "file is corrupt and cannot be opened."

标签 c# ms-word openxml

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

最佳答案

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

关于c# - C# 中的 Word 文档的 OpenXML 错误 "file is corrupt and cannot be opened.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5346409/

相关文章:

c# - 使形状最顶层

asp.net - 如何使用 open xml 将数据表单数据库绑定(bind)到 powerpoint 中的现有表

c# - 如何在 Asp.Net Core 1.0.0 中本地化显示属性?

c# - 在 .NET 4.6 项目中使用 Google.Protobuf

c# - 迁移到 OAuth 2.0 后获取 access_token 时出现问题

c# - 将 Entity FrameWork 与多个 MS SQLSERVER 数据库一起使用

python - 在 docx 中按顺序处理对象

c# - VSTO/字 : How to freeze a document window?

c# - 如何在 DocumentFormat.OpenXml 电子表格中添加行/单元格?

c# - 数字存储为文本