[英]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.