简体   繁体   English

为什么在添加嵌套表格时出现“Word 在 .doc 中发现无法读取的内容”

[英]Why do I get 'Word found unreadable content in .doc' when adding a nested table

I am using OpenXML to build a word document with a nested table, ie a table within a table.我正在使用 OpenXML 构建一个带有嵌套表格的 word 文档,即表格中的表格。 However, adding the nested table results in a 'Word found unreadable content in.doc' pop-up message when trying to open the file in Word for MS 365.但是,当尝试在 Word for MS 365 中打开文件时,添加嵌套表格会导致“Word 在 .doc 中发现不可读的内容”弹出消息。

Removing the nested table, or replacing it with text, results in a file that can be opened without issue.删除嵌套表格或将其替换为文本会生成一个可以毫无问题地打开的文件。

Recovering the file in Word for MS 365 by clicking 'Ok' on the pop up which displays 'Do you want to recover the contents of this document?'通过在显示“是否要恢复此文档的内容?”的弹出窗口中单击“确定”来恢复 Word for MS 365 中的文件。 results in a file that can be opened and looks correct, but that isn't a viable solution.生成一个可以打开并且看起来正确的文件,但这不是一个可行的解决方案。

I've looked at the XML generated by the recovered version of the file, but quite a lot of changes (files like settings.xml and webSettings.xml are added, for example) that don't appear directly relevant to having a table within a table.我查看了文件的恢复版本生成的 XML,但是有很多更改(例如,添加了 settings.xml 和 webSettings.xml 等文件)似乎与在其中放置表格没有直接关系一张桌子。

Here is the code used to generate the MemoryStream of the doc with a table within a table:下面是用于生成带有表中表的文档的 MemoryStream 的代码:

 MemoryStream mem = new MemoryStream();
           
// Create Document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
{
    // Add a main document part. 
    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

    // Create the document structure and add some text.
    mainPart.Document = new Document();
    mainPart.Document.Body = new Body();
    Body docBody = mainPart.Document.Body;

    Table formTable = new Table();

    string sectionName = "Section Name like Core Information";
    FontSize fontSize = new FontSize();
    fontSize.Val = "30";

    RunProperties runProp = new RunProperties(fontSize);
    Run tableRun = new Run(runProp);
    tableRun.AppendChild(new Text(sectionName));
    TableCell tableCell = new TableCell(new Paragraph(tableRun));

    TableCellProperties tblCellProperties = new TableCellProperties(new TableCellWidth() { Width = "5000", Type = TableWidthUnitValues.Pct });
    tableCell.Append(tblCellProperties);

    TableRow sectionRow = new TableRow(tableCell);


    TableCell alternativeCell = new TableCell();
    Table subTable = new Table();
    Run subtableRun = new Run(runProp.CloneNode(true));
    subtableRun.AppendChild(new Text("Sub Table Run"));
    TableCell subTableCell = new TableCell(new Paragraph(subtableRun));
    TableCellProperties subTblCellProperties = new TableCellProperties(new TableCellWidth() { Width = "5000", Type = TableWidthUnitValues.Pct });
    subTableCell.Append(subTblCellProperties);
    TableRow subTableRow = new TableRow(subTableCell);
    subTable.AppendChild(subTableRow);
    alternativeCell.Append(subTable);
    TableRow RowHousingSubTable = new TableRow(alternativeCell);
    // make a subform i.e. sub table here

    formTable.AppendChild(sectionRow);
    formTable.AppendChild(RowHousingSubTable);

    docBody.Append(formTable);
}
return mem;

I then save the MemoryStream to disk with然后我将 MemoryStream 保存到磁盘

var bytes = stream.ToArray();
File.WriteAllBytes(@"C:\temp\word_crash.docx", bytes);

Adding an empty paragraph after the nested table will fix this, ie在嵌套表格后添加一个空段落将解决这个问题,即

 alternativeCell.Append(subTable);
 alternativeCell.Append(new Paragraph()); // add this line

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

相关问题 Word打开文件时发现内容不可读错误 - Word found unreadable content error when opening file 当使用Epplus的小计功能时,我得到“Excel发现不可读的内容......”错误 - When use subtotal function with Epplus, I get “Excel found unreadable content in…” error OpenXml Dotx 到 Docx Word 发现不可读的内容 - OpenXml Dotx to Docx Word Found Unreadable Content 添加到Word文档时图片重叠 - Pictures overlapping when adding to word doc Excel Open错误:创建Excel时“发现不可读的内容” - Excel Open error: “found unreadable content” when creating Excel Word 在...中发现无法读取的内容...正在从 C# 应用程序以 word 格式从 SSRS 下载文件 - Word found unreadable content in... Downloading file from SSRS in word format from C# app Excel 在 *.xlsx 中发现不可读的内容 - Excel found unreadable content in *.xlsx 使用 C# Web API 下载文件后出现“Word 发现无法读取的内容” - "Word found unreadable content" after downloading file with C# Web API 如何隐藏在C#中打开Word文档时显示的VIsual Basic“禁用宏”对话框 - HOw do I suppress the VIsual Basic “Macros are Disabled” Dialog that shows when opening a word doc in C# 当用户上传单词文档(.doc / .docx)时,如何计算单词文档中的单词数? - How do I count the number of words in a word document (.doc / .docx) when a user uploads it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM