c# - Word 在打开文件时发现无法读取的内容错误
问题描述
我在打开 Word (.docx) 文件时遇到错误“ Word 在 abc.docx 中发现不可读的内容。您要恢复此文档的内容吗? ”。
我已经尝试过互联网上给出的所有解决方案,但没有成功。下面是我将内容写入流的代码。
private void test()
{
using (MemoryStream one = await db.DownloadFile("templates", "one.docx"))
{
using (MemoryStream two = await db.DownloadFile("templates", "two.docx"))
{
using (MemoryStream newStream = new MemoryStream())
{
one.CopyTo(newStream);
editingMemoryStream.Position = 0;
using (WordprocessingDocument mainDoc = WordprocessingDocument.Open(one, true))
{
using (WordprocessingDocument newDoc =
WordprocessingDocument.Open(newStream, true))
{
Generate(modal, new, main, report);
}
}
}
}
}
}
private void Generate(List<modal> mo, WordprocessingDocument new, MemoryStream report)
{
var main = new.MainDocumentPart;
modal = mo[0];
AddTableToBody(report, modal.table, mo);
}
public void AddTableToBody(MemoryStream temp, dailyReport,
MainDocumentPart main)
{
using (WordprocessingDocument newDoc = WordprocessingDocument.Open(editingMemoryStream, true))
{
WP.Body body= dailyReport.MainDocumentPart.Document.Body;
var main = dailyReport.MainDocumentPart;
//* some code is here*//
var clone = dailyReport.CloneNode(true);
main.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
main.Document.Save();
}
}
该文件包含 2 个带有一些标签的表格。
解决方案
查看您的代码,您似乎在以下两行代码中将元素(实例)添加到元素(实例w:body
)Body
中w:r
:Run
var clone = dailyReport.CloneNode(true);
main.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
在上面的摘录中,clone
是一个Body
实例(w:body
元素)及其所有子元素。我只能假设parent
是MainDocumentPart
其他的WordprocessingDocument
。您正在附加一个带有一个 ( ) 的新( ) 和一个( ) 以及Paragraph
后者具有的任何子级。这是无效的 Open XML,很可能是 Word 抱怨的原因。w:p
w:r
Run
w:body
Body
推荐阅读
- node.js - SSH命令输出到终端但不是控制台登录Electron
- r - R更改基础包功能
- laravel - laravel 获取带有计数的数据库的结果
- php - PHP CURL API 令牌检索
- javascript - cypress.io:命令命名空间、链接
- header-files - 如何使用头文件
使用 Yocto 生成的 SDK? - docker - 使用 ECS 任务 ID 命名 Sidekiq 工作进程
- php - Composer Require 'package' 引发 OpenSSL 错误
- asp.net-core - 为什么 .Net Core 2.1.1 通用主机需要庞大的 Microsoft.AspNetCore.App 元包才能工作?
- python - PyQt5 在 dbus 请求失败时创建错误