簡體   English   中英

OpenXML append 行到 Word 中的現有表

[英]OpenXML append rows to an existing table in Word

我想將 append 行添加到僅具有 header 行的現有表中。 我使用 OpenXML,但由於錯誤 0x80004005,我無法打開 Word 文檔。 這是我認為還可以的來源,但是...:

public static byte[] WordDocument(List<List<string>> data)
{
  using (MemoryStream mem = new MemoryStream())
  {
    byte[] byteArray = File.ReadAllBytes(templatePath);
    mem.Write(byteArray, 0, (int)byteArray.Length);
    using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(mem, true))
    {
      Table table = wordDocument.MainDocumentPart.Document.Body.Elements<Table>().First();
      foreach (var line in data)
      {
        TableRow tr = new TableRow();
        foreach (var column in line)
        {
          TableCell tc = new TableCell(new Paragraph(new Run(new Text(column))));
          tr.Append(tc);
        }
        table.Append(new TableRow());
      }
    }
    return mem.ToArray();
  }
}

document.xml: PasteBin

寫入 stream 后倒帶。 出於興趣,為什么要使用 stream ,而不是直接從文檔加載?

那不應該是table.Append(tr); 而不是table.Append(new TableRow()); ?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM