博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net用NPOI生成Word表格
阅读量:5015 次
发布时间:2019-06-12

本文共 7368 字,大约阅读时间需要 24 分钟。

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                 IList
wlist = 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年版的,有需要的自己去下吧

转载于:https://www.cnblogs.com/ymworkroom/p/6786538.html

你可能感兴趣的文章
Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性
查看>>
Makefile ===> Makefile 快速学习
查看>>
face detection[HR]
查看>>
java性能调优工具
查看>>
C# 其他的Url 文件的路径转化为二进制流
查看>>
cmake使用
查看>>
ios7上隐藏status bar
查看>>
构造方法和全局变量的关系
查看>>
python3基础05(有关日期的使用1)
查看>>
ArrayList的使用方法
查看>>
面向对象高级
查看>>
Bitwise And Queries
查看>>
打印Ibatis最终的SQL语句
查看>>
HBase之八--(3):Hbase 布隆过滤器BloomFilter介绍
查看>>
oracle连接问题ORA-00604,ORA-12705
查看>>
NOI 2019 退役记
查看>>
java的几个日志框架log4j、logback、common-logging
查看>>
Java从零开始学十三(封装)
查看>>
Python2和Python3中的rang()不同之点
查看>>
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)...
查看>>