NPOI读取或生成Excel是非常容易弄懂和实现的,但是生成word表格就不容易了,特别是行列合并,以及同一个单元格内用多种样式,以下是我在项目中生成word表格时的部分代码,记下来备忘。
1 CT_SectPr srcpr = new CT_SectPr(); 2 //设置A4纸纵向,如果要横向,两个值调换即可 3 srcpr.pgSz.w = (ulong)11906; 4 srcpr.pgSz.h = (ulong)16838; 5 6 XWPFDocument doc = new XWPFDocument(); 7 doc.Document.body.sectPr = srcpr; 8 //输出标题 9 XWPFParagraph ptitle = doc.CreateParagraph(); 10 ptitle.Alignment = ParagraphAlignment.CENTER; 11 XWPFRun titlerun = ptitle.CreateRun(); 12 titlerun.SetText(string.Format("{0}营销考勤与工作计划",para.date)); 13 titlerun.SetBold(true); 14 titlerun.FontFamily = "华文行楷"; 15 titlerun.FontSize = 30; 16 titlerun.SetColor("blue"); 17 18 WorkPlanService wbll = new WorkPlanService(); 19 XWPFParagraph tipm = doc.CreateParagraph(); 20 tipm.Alignment = ParagraphAlignment.CENTER; 21 XWPFRun tiprun = tipm.CreateRun(); 22 tiprun.SetText(string.Format("生成时间:{0}", dtime.ToString("yyyy-MM-dd HH:mm"))); 23 tiprun.FontSize = 15; 24 titlerun.FontFamily = "宋体"; 25 CT_Row ctrow = null; 26 XWPFTableRow mrow = null; 27 XWPFTableCell cell = null; 28 CT_Tc cttc = null; 29 CT_TcPr ctpr = null; 30 CT_P ctp = null; 31 XWPFParagraph cp = null; 32 XWPFRun xtrun = null; 33 //取这天的工作计划 34 //取工作计划 35 WorkPlanBM wbm = null; 36 IListwlist = null; 37 foreach (var sm in slist) 38 { 39 List flist = list.Where(m => m.UserNo == sm.UserNo).ToList (); 40 if (flist == null || flist.Count == 0) continue; 41 XWPFTable tab = doc.CreateTable(); 42 tab.Width = 5100; 43 foreach (var fm in flist) 44 { 45 string dktimeStr = string.Format("{0}", BaseMethod.FormatDate(fm.DkTime, "yyyy-MM-dd HH:mm")); 46 string dkAddr = string.Format("{0}", fm.Addr); 47 MyOperationResult mr = wbll.GetSingle(sm.UserNo, 0, false, ref wbm, ref wlist); 48 string planStr = string.Empty; 49 if (wlist == null) 50 { 51 planStr = string.Format("没有写计划或者有计划没有提交!"); 52 } 53 else 54 { 55 StringBuilder psb = new StringBuilder(); 56 foreach (var pm in wlist) 57 { 58 psb.Append(pm.PlanTitle); 59 } 60 planStr = psb.ToString(); 61 } 62 ctrow = new CT_Row(); 63 if (tab.Rows.Count == 1 && tab.GetRow(0).GetTableCells().Count == 1) 64 { 65 mrow = tab.GetRow(0); 66 cell = mrow.GetCell(0); 67 } 68 else 69 { 70 mrow = new XWPFTableRow(ctrow, tab); 71 tab.AddRow(mrow); 72 cell = mrow.CreateCell(); 73 } 74 cttc = cell.GetCTTc(); 75 ctpr = cttc.AddNewTcPr(); 76 ctpr.AddNewVMerge().val = ST_Merge.restart;//注意:合并行时,第一行用ST_Meger.restart,后面的被合并的列用ST_Merge.@continue 77 ctpr.AddNewVAlign().val = ST_VerticalJc.center; 78 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; 79 ctp = cttc.GetPList()[0]; 80 cp = cell.GetParagraph(ctp); 81 xtrun = cp.CreateRun(); 82 xtrun.SetText(string.Format("{0}", sm.UserName)); 83 cp = cell.AddParagraph(); 84 cp.Alignment = ParagraphAlignment.CENTER; 85 xtrun = cp.CreateRun(); 86 xtrun.SetText(string.Format("{0}", sm.UserNo)); 87 88 cell = mrow.CreateCell(); 89 cttc = cell.GetCTTc(); 90 ctpr = cttc.AddNewTcPr(); 91 ctp = cttc.GetPList()[0]; 92 cp = cell.GetParagraph(ctp); 93 xtrun = cp.CreateRun(); 94 xtrun.SetText("打卡时间:"); 95 xtrun.SetBold(true); 96 xtrun = cp.CreateRun(); 97 xtrun.SetText(dktimeStr); 98 //cttc.GetPList()[0].AddNewR().AddNewT().Value = dktimeStr; 99 100 ctrow = new CT_Row();101 mrow = new XWPFTableRow(ctrow, tab);102 tab.AddRow(mrow);103 cell = mrow.CreateCell();104 cttc = cell.GetCTTc();105 ctpr = cttc.AddNewTcPr();106 ctpr.AddNewVMerge().val = ST_Merge.@continue;107 108 cell = mrow.CreateCell();109 cttc = cell.GetCTTc();110 ctpr = cttc.AddNewTcPr();111 ctp = cttc.GetPList()[0];112 cp = cell.GetParagraph(ctp);113 xtrun = cp.CreateRun();114 xtrun.SetText("打卡地点:");115 xtrun.SetBold(true);116 xtrun = cp.CreateRun();117 xtrun.SetText(dkAddr);118 //cell.SetText(dkAddr);119 120 ctrow = new CT_Row();121 mrow = new XWPFTableRow(ctrow, tab);122 tab.AddRow(mrow);123 cell = mrow.CreateCell();124 cttc = cell.GetCTTc();125 ctpr = cttc.AddNewTcPr();126 ctpr.AddNewVMerge().val = ST_Merge.@continue;127 128 cell = mrow.CreateCell();129 cttc = cell.GetCTTc();130 ctpr = cttc.AddNewTcPr();131 ctp = cttc.GetPList()[0];132 cp = cell.GetParagraph(ctp);133 xtrun = cp.CreateRun();134 xtrun.SetText("工作计划:");135 xtrun.SetBold(true);136 xtrun = cp.CreateRun();137 xtrun.SetText(planStr);138 //cell.SetText(planStr);139 }140 XWPFParagraph p = doc.CreateParagraph();
下面是生成的效果图:
用的dll是从github上下载的最新的,其实也不新了,2015年版的,有需要的自己去下吧