【发布时间】:2015-05-19 04:53:22
【问题描述】:
我有一个 word 文档,其中添加了表格。我想访问该文档并将新的空白行添加到已经在其中的表中。我参考了this 参考链接并创建了以下代码:
static void Main(string[] args)
{
string filePath = "C:\\TestDoc1.docx";
byte[] byteArray = File.ReadAllBytes(filePath);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.GetFirstChild<TableRow>().Descendants<TableCell>().Count() == 4))
{
// Create a row.
TableRow tr = new TableRow();
t.Append(tr);
}
}
// Save the file with the new name
File.WriteAllBytes("C:\\TestDoc2.docx", stream.ToArray());
}
}
但是,代码不会引发任何错误。但是当我打开 TestDoc2.docx 时,我收到以下错误:
我错过了什么?
【问题讨论】: