NPOI给word中的表格table动态添加行,并保留表格样式 , 替换文字
public class NPOIHelper { /// <summary> /// 输出模板docx文档 /// </summary> /// <param name="tempFilePath">模板文件地址</param> /// <param name="outFolder">输出文件夹</param > /// <param name="fileName">文件名</param> public static void CreateWord(string tempFilePath, string outFolder, string fileName, DataDto data) { List<DataTable> dt = data.DataTables; using (FileStream stream = File.OpenRead(tempFilePath)) { XWPFDocument doc = new XWPFDocument(stream); //遍历段落 foreach (var para in doc.Paragraphs) { ReplaceKey(para, data.ParagraphsModel); } //遍历表格 var tables = doc.Tables; foreach (var table in tables) { foreach (var row in table.Rows) { foreach (var cell in row.GetTableCells()) { foreach (var para in cell.Paragraphs) { ReplaceKey(para, data.ParagraphsModel); } } } } // DataView dvResult = dt4.DefaultView; #region 表格 int iTable = 0; //第几张表 //1.循环Word文档中的表格 foreach (XWPFTable table in doc.Tables) { if (dt.Count > iTable) { //重点新增行 CT_Row ctrow = table.GetRow(1).GetCTRow(); table.RemoveRow(table.Rows.IndexOf(table.GetRow(1))); //先移除模板行 int newRowCnt = dt[iTable].Rows.Count; for (int j = 0; j < newRowCnt; j++) { CT_Row targetRow = new CT_Row(); //复制cell结构 foreach (CT_Tc item in ctrow.Items) { CT_Tc addTc = targetRow.AddNewTc(); addTc.tcPr = item.tcPr;//cell样式,只包括列宽和cell对齐方式 IList<CT_P> list_p = item.GetPList(); foreach (var p in list_p) { CT_P addP = addTc.AddNewP(); addP.pPr = p.pPr;//段落样式 IList<CT_R> list_r = p.GetRList(); foreach (CT_R r in list_r) { CT_R addR = addP.AddNewR(); addR.rPr = r.rPr;//run样式 包括字体等 List<CT_Text> list_text = r.GetTList(); foreach (CT_Text text in list_text) { CT_Text addText = addR.AddNewT(); addText.space = text.space; addText.Value = text.Value; } } } } //增加数据行 XWPFTableRow mrow = new XWPFTableRow(targetRow, table); table.AddRow(mrow); } int iRow = 1; bool flag = true; foreach (XWPFTableRow row in table.Rows) { if (flag) { flag = false; continue; } //var iRow = table.Rows.IndexOf(row); //表中行的循环索引 if (iRow > 0) { //3.循环没行中的列 foreach (XWPFTableCell cell in row.GetTableCells()) { var iCell = row.GetTableCells().IndexOf(cell); //表中列的循环索引 //4.进行单元格中内容的获取操作 //4.1获取单元格中所有的XWPFParagraph(单元格中每行数据都是一个XWPFParagraph对象) IList<XWPFParagraph> listXWPFParagraph = cell.Paragraphs; //第一个XWPFParagraph XWPFParagraph xwpfPCource = listXWPFParagraph[0]; if (xwpfPCource != null) { //获取现有的Run集合 IList<XWPFRun> listRun = xwpfPCource.Runs; //循环移除 while (listRun.Count > 0) { xwpfPCource.RemoveRun(0); } //添加获取的数据 XWPFRun xwpgRScience = xwpfPCource.CreateRun(); xwpgRScience.FontFamily = "宋体"; xwpgRScience.SetText( dt[iTable].Rows[iRow - 1][iCell].ToString()); xwpgRScience.FontSize = 12; xwpfPCource.AddRun(xwpgRScience); } else { cell.RemoveParagraph(1); } } iRow++; } } } iTable++; } if (!Directory.Exists(outFolder)) { Directory.CreateDirectory(outFolder); } var fullPath = Path.Combine(outFolder, fileName); FileStream outFile = new FileStream(fullPath, FileMode.Create); doc.Write(outFile); outFile.Close(); #endregion } } /// <summary> /// 遍历替换段落位置字符 /// </summary> /// <param name="para">段落参数</param> /// <param name="model">数据</param> private static void ReplaceKey(XWPFParagraph para, object model) { string text = para.ParagraphText; var runs = para.Runs; string styleid = para.Style; for (int i = 0; i < runs.Count; i++) { var run = runs[i]; text = run.ToString(); Type t = model.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo p in pi) { //$$与模板中$$对应,也可以改成其它符号,比如{$name},务必做到唯一 if (text.Contains("{$" + p.Name + "}")) { //<br/>换行 if (p.GetValue(model, null).ToString().Contains("<br/>")) { string[] strs = Regex.Split(p.GetValue(model, null).ToString(), "<br/>", RegexOptions.IgnoreCase); runs[i].SetText(strs[0], 0); for(int j=1;j<strs.Length-1; j++) { run.AddCarriageReturn(); runs[i].AppendText(strs[j]); } } else { text = text.Replace("{$" + p.Name + "}", p.GetValue(model, null)?.ToString()); runs[i].SetText(text, 0); } } } } } }
参考文章
https://www.cnblogs.com/nora/p/13229176.html
https://www.cnblogs.com/fger/p/11187954.html
https://www.cnblogs.com/masonblog/p/7097483.html
https://www.cnblogs.com/chy386/p/12837773.html