在使用NPOI操作制作word数据模板时,需要在word中插入表格,包括合并单元格的方式
图例:
1,没有合并单元的方式创建表格,这种方式是提前创建好所需要的表格,然后再往里面填数
XWPFDocument doc= new XWPFDocument(); //创建表格-提前创建好表格后填数 XWPFTable tableContent = doc.CreateTable(4, 5);//4行5列 tableContent.Width = 1000 * 5; tableContent.SetColumnWidth(0, 1000);/* 设置列宽 */ tableContent.SetColumnWidth(1, 1500); tableContent.SetColumnWidth(2, 1500); tableContent.SetColumnWidth(3, 1000); tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "地点")); tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, tableContent, "日期")); tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "男性")); tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc, tableContent, "女性")); tableContent.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc, tableContent, "合计")); //测试数据格式 //string[,] siblings = new string[3, 5] { { "航天桥", "-", "0", "0", "0" }, { "马甸", "-", //"0", "0", "0" }, { "洋桥", "04月16日 - 05月31日", "0", "0", "0" } }; //List<ArrayList> list=Common.PubVars.listTable; for (int i = 0; i < list.Count;i++)//有3个数组 { ArrayList ls=list[i]; for (int j = 0; j < ls.Count;j++) {
tableContent.GetRow(i + 1).GetCell(j).SetParagraph(SetCellText(doc, tableContent, ls[j].ToString())); } }
//设置字体样式
public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText) { //table中的文字格式设置 CT_P para = new CT_P(); XWPFParagraph pCell = new XWPFParagraph(para, table.Body); pCell.Alignment = ParagraphAlignment.CENTER;//字体居中 pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中 XWPFRun r1c1 = pCell.CreateRun(); r1c1.SetText(setText); r1c1.FontSize = 12; r1c1.FontFamily = "华文楷体"; //r1c1.SetTextPosition(20);//设置高度 return pCell; }
2,合并单元格的方式,这种方式需要一行一行的创建并操作合并,填入数据,
//数据样例 //string[,] siblings = new string[10, 7] { //{ "1", "test1", "0", "0", "0","0","0" }, //{ "2", "test2", "0", "0", "0","0", "0" }, //{ "3", "test3", "0", "0", "0","0", "0"}, //{ "4", "test4", "0", "0", "0","0", "0"}, //{ "5", "test5", "0", "0", "0","0", "0"}, //{ "6", "test6", "0", "0", "0","0", "0"}, //{ "7", "test7", "0", "0", "0","0", "0"}, //{ "8", "test8", "0", "0", "0","0", "0"}, //{ "9", "test9", "0", "0", "0","0", "0"}, //{ "10","test10", "0", "0", "0","0", "0"}, //}; List<ArrayList> listYcTable=new List<ArrayList>(); int rows = list.Count; int col = 0; foreach (ArrayList al in list) { col = al.Count;//列数 } //创建表格 XWPFTable table = doc.CreateTable(1, col);//思路,数据一行一行画 table.RemoveRow(0);//去掉第一行空白的 table.Width = 1000 * 5; table.SetColumnWidth(0, 300);/* 设置列宽 */ table.SetColumnWidth(1, 1200); for (int i = 0; i < col - 3; i++) { table.SetColumnWidth(2 + i, 800);/* 设置列宽 */ } table.SetColumnWidth(col - 1, 700); CT_Row nr = new CT_Row(); XWPFTableRow mr = new XWPFTableRow(nr, table);//创建行 table.AddRow(mr);//将行添加到table中 XWPFTableCell c1 = mr.CreateCell();//创建单元格 CT_Tc ct = c1.GetCTTc(); CT_TcPr cp = ct.AddNewTcPr(); //第1行 cp.AddNewVMerge().val = ST_Merge.restart;//合并行 cp.AddNewVAlign().val = ST_VerticalJc.center;//垂直 ct.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; ct.GetPList()[0].AddNewR().AddNewT().Value = "序号"; c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.AddNewVMerge().val = ST_Merge.restart;//合并行 cp.AddNewVAlign().val = ST_VerticalJc.center;//垂直 ct.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; ct.GetPList()[0].AddNewR().AddNewT().Value = "指标名称"; c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.gridSpan = new CT_DecimalNumber(); cp.gridSpan.val = Convert.ToString(col - 3); //合并列 cp.AddNewVAlign().val = ST_VerticalJc.center; ct.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//单元格内容居中显示 ct.GetPList()[0].AddNewR().AddNewT().Value = "年龄段"; c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.AddNewVMerge().val = ST_Merge.restart;//合并行 cp.AddNewVAlign().val = ST_VerticalJc.center;//垂直 ct.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; ct.GetPList()[0].AddNewR().AddNewT().Value = "合计"; //=====第一行表头结束========= //2行,多行合并类似 nr = new CT_Row(); mr = new XWPFTableRow(nr, table); table.AddRow(mr); c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.AddNewVMerge().val = ST_Merge.@continue;//合并行 序号 c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.AddNewVMerge().val = ST_Merge.@continue;//合并行 指标名称 //年龄段分组 //["20岁以下","21-30","31-40","41-50","51-60","70岁以上"] ArrayList alAge = Common.PubVars.ageGroup;//年龄段数组 for (int i = 0; i < alAge.Count; i++) { mr.CreateCell().SetText(Convert.ToString(alAge[i]));//年龄段单元格 } c1 = mr.CreateCell();//创建单元格 ct = c1.GetCTTc(); cp = ct.AddNewTcPr(); cp.AddNewVMerge().val = ST_Merge.@continue;//合并行 合计 //====第二行要合并的 for (int i = 0; i < list.Count; i++) { //创建一行 nr = new CT_Row(); mr = new XWPFTableRow(nr, table); table.AddRow(mr); for (int j = 0; j < list[i].Count; j++) { //每一行创建对应的数据 mr.CreateCell().SetText(list[i][j].ToString()); // table.GetRow(i + 1).GetCell(j).SetParagraph(SetCellText(doc, table, list[i][j].ToString())); } }
合并单元格有跨列合并,和跨行分部,两个关键点:
跨列合并
cp.gridSpan = new CT_DecimalNumber();//实例化数字必须 cp.gridSpan.val = “3”; //合并列 合并3列
跨行合并
cp.AddNewVMerge().val = ST_Merge.restart;//合并行--开始行 cp.AddNewVMerge().val = ST_Merge.@continue;//合并行--到合并行
//合并的单元格–边创建边合并
最新评论