C# 无法使用WordprocessingDocument将新行添加到word文档中的现有表中

C# 无法使用WordprocessingDocument将新行添加到word文档中的现有表中,c#,ms-word,openxml,C#,Ms Word,Openxml,我有一个word文档,其中添加了表。我想访问该文档,并将新的空行添加到已在其中的表中。我参考了参考链接并创建了以下代码: static void Main(string[] args) { string filePath = "C:\\TestDoc1.docx"; byte[] byteArray = File.ReadAllBytes(filePath); using (MemoryStream stream = new MemoryStream()) {

我有一个word文档,其中添加了表。我想访问该文档,并将新的空行添加到已在其中的表中。我参考了参考链接并创建了以下代码:

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());
    }
} 
using(DocX doc = DocX.Load(filePath))
{
    // you can use whatever condition you would like to select the table from that table.
   // I am using the Title field value in the Table Properties wizard under Alter Text tab 
    Novacode.Table t = doc.Tables.Cast<Table>().FirstOrDefault(tbl => tbl.TableCaption == "Test");
    Row r = t.InsertRow();
    doc.SaveAs("C:\\TestDoc2.docx");
}
static void Main(字符串[]args)
{
string filePath=“C:\\TestDoc1.docx”;
byte[]byteArray=File.ReadAllBytes(文件路径);
使用(MemoryStream stream=new MemoryStream())
{
stream.Write(byteArray,0,(int)byteArray.Length);
使用(WordprocessingDocument doc=WordprocessingDocument.Open(stream,true))
{
Body bod=doc.main documentpart.Document.Body;
foreach(bod.substands()中的表t,其中(tbl=>tbl.GetFirstChild().substands().Count()==4))
{
//创建一行。
TableRow tr=新的TableRow();
t、 追加(tr);
}
}
//用新名称保存文件
File.writealBytes(“C:\\TestDoc2.docx”,stream.ToArray());
}
} 
但是,代码不会抛出任何错误。但是当我打开TestDoc2.docx时,我得到了以下错误:


我缺少什么?

我发现了一个code plex helper dll,它快速、轻量级,并且不需要安装Microsoft Word或Office。你可以从我这里得到。将此dll的引用添加到解决方案中,并编写以下代码:

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());
    }
} 
using(DocX doc = DocX.Load(filePath))
{
    // you can use whatever condition you would like to select the table from that table.
   // I am using the Title field value in the Table Properties wizard under Alter Text tab 
    Novacode.Table t = doc.Tables.Cast<Table>().FirstOrDefault(tbl => tbl.TableCaption == "Test");
    Row r = t.InsertRow();
    doc.SaveAs("C:\\TestDoc2.docx");
}
使用(DocX doc=DocX.Load(filePath))
{
//您可以使用希望从该表中选择表的任何条件。
//我正在使用“表格属性向导”中“更改文本”选项卡下的“标题”字段值
Novacode.Table t=doc.Tables.Cast().FirstOrDefault(tbl=>tbl.TableCaption==“Test”);
行r=t.InsertRow();
doc.SaveAs(“C:\\TestDoc2.docx”);
}
对我来说,它就像一个符咒!!!:-)


希望这对其他人也有帮助。

您的第一段代码中缺少的是一个表格单元格和一个段落

// Create a row.
TableRow tr = new TableRow(new TableCell(new Paragraph()));
t.Append(tr);
TableRow必须至少包含一个TableCell,TableCell必须包含一个段落

// Create a row.
TableRow tr = new TableRow(new TableCell(new Paragraph()));
t.Append(tr);