private XWPFDocument InsertTable(XWPFDocument doc, Table t) { var maxColCount = t.Rows.Max(x => x.Cells.Count); if (t == null) { return(doc); } var table = doc.CreateTable(); table.Width = t.Width; int index = 0; t.Rows?.ForEach(r => { XWPFTableRow tableRow = index == 0 ? table.GetRow(0) : table.CreateRow(); for (int i = 0; i < r.Cells.Count; i++) { var cell = r.Cells[i]; var xwpfCell = i == 0 ? tableRow.GetCell(0) : tableRow.AddNewTableCell(); foreach (var para in cell.Paragraphs) { xwpfCell.AddParagraph().Set(para); } if (!string.IsNullOrWhiteSpace(cell.Color)) { tableRow.GetCell(i).SetColor(cell.Color); } } //补全单元格,并合并 var rowColsCount = tableRow.GetTableICells().Count; if (rowColsCount < maxColCount) { for (int i = rowColsCount - 1; i < maxColCount; i++) { tableRow.CreateCell(); } tableRow.MergeCells(rowColsCount - 1, maxColCount); } index++; }); return(doc); }