您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C# 使用NPOI生成Word文档(按照模板)

2025/10/19 23:16:50发布31次查看
项目需要集成word导出,做的时候网上文档资源不是很多,也比较杂乱,所以查了查,整理了整理,做个记录,也顺便把npoi操作word文档的一些基本操作分享给有需要的朋友。
本篇包括生成word对word文本的操作,表格的操作,以及图片的操作,都为生成word基础的一些操作。
以下只是我的个人理解所得,大家有什么更好的想法欢迎补充。
vs2017、右键解决方案,管理nuget程序包,搜索并为项目安装npoi程序包,并引用:
using npoi.xwpf.usermodel;
本编使用npoi版本为2.3.0
下面进入正题··
一、    获取模板(xwpfdocument doc)
使用模板,首先获取模板,然后把获取的模板实例化为npoi的文档对象进行编辑操作:
using (filestream stream = file.openread(“模板文件地址”)){
   xwpfdocument doc = new xwpfdocument(stream);
   //处理doc,代码控制编辑文档。
}
处理doc后,生成新的文件,写入doc ,生成word完成。
filestream file = new filestream(生成文件路径+文件名, filemode.create, fileaccess.write);
doc.write(file);
file.close();
doc就是我们获取到的模板的所有内容。
还有一点本编使用模板均为docx后缀文件,doc修改docx读取报错,需要另存为doxc文档。
二、    文本处理(xwpfparagraph para)
doc.paragraphs  获取到文档里的所有的段落对象;
para.paragraphtext  获取段落的文本数据;
para.replacetext(要被替换的文本,替换文本)  替换段落的文本(模板能实现的关键)
xwpfparagraph的官方参考文档
三、    表格处理(xwpftable table)
doc.tables      获取文档里的所有的表格对象;
这里有必要多一嘴,doc.tables获取的只是word中最外层的表格,不包含嵌套内层的。
获取嵌套单元格可使用cell.tables;
(一)   表格行处理(xwpftablerow row)
row.rows        获取表格所有行;
(二)   表格单元格处理(xwpftablecell cell)
row.gettableicells()   ;获取表格行的所有单元格;
获取到单元格之后就可以获取单元格里的文本段落(paragraphs)并且进行文本替换
(三)   水平合并行单元格
ct_tc cttcofrowthird = cell.getcttc();ct_tcpr ctprofrowthird = cttcofrowthird.addnewtcpr();ctprofrowthird.gridspan = new ct_decimalnumber();ctprofrowthird.gridspan.val = num.tostring();//合并num列
(四) 垂直合并列单元格
list<xwpftablerow> rows所有要合并的行的xwpftablerow对象集合。xwpftablecell cellfirstofthird = 第一行要合并的单元格对象;ct_tc cttcfirstofthird = cellfirstofthird.getcttc();ct_tcpr ctprfirstofthird = cttcfirstofthird.addnewtcpr();ctprfirstofthird.addnewvmerge().val = st_merge.restart;//开始合并行ctprfirstofthird.addnewvalign().val = st_verticaljc.center;//垂直cttcfirstofthird.getplist()[0].addnewppr().addnewjc().val = st_jc.center;for (int i = 1; i < rows.count; i++){ xwpftablecell cellfirstofrow = 第i行要合并的单元格对象; ct_tc cttcfirstofrow = cellfirstofrow.getcttc(); ct_tcpr ctprfirstofrow = cttcfirstofrow.addnewtcpr(); ctprfirstofrow.addnewvmerge().val = st_merge.@continue;//继续合并行 ctprfirstofrow.addnewvalign().val = st_verticaljc.center;//垂直}
四、 图片处理
2.3.0版本的npoi的图片插入没有集成xml文件的修改所以需要手写代码编入(当然,我是拷过来的)。
using (filestream fsimg = new filestream(图片路径, filemode.open, fileaccess.read, fileshare.none)){var picid = doc.addpicturedata(fsimg, (int)npoi.xwpf.usermodel.picturetype.jpeg);string picxml = ""+ " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + " <pic:nvpicpr>" + " <pic:cnvpr id=\"" + "0" + "\" name=\"generated\"/>" + " <pic:cnvpicpr/>" + " </pic:nvpicpr>" + " <pic:blipfill>" + " <a:blip r:embed=\"" + id + "\" xmlns:r=\"http://schemas.openxmlformats.org/officedocument/2006/relationships\"/>" + " <a:stretch>" + " <a:fillrect/>" + " </a:stretch>" + " </pic:blipfill>" + " <pic:sppr>" + " <a:xfrm>" + " <a:off x=\"0\" y=\"0\"/>" + " <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" + " </a:xfrm>" + " <a:prstgeom prst=\"rect\">" + " <a:avlst/>" + " </a:prstgeom>" + " </pic:sppr>" + " </pic:pic>"; xwpfparagraph par = cell.addparagraph();//创建段落对象(可以在doc加也可在cell加) par.alignment = paragraphalignment.center;//居中 xwpfrun run = par.createrun(); ct_inline inline = run.getctr().addnewdrawing().addnewinline(); inline.graphic = new ct_graphicalobject { graphicdata = new ct_graphicalobjectdata { uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" } }; try { inline.graphic.graphicdata.addpicelement(picxml); } catch (xmlexception xe) { throw xe; } npoi.openxmlformats.dml.wordprocessing.ct_positivesize2d extent = inline.addnewextent(); extent.cx = width; extent.cy = height; npoi.openxmlformats.dml.wordprocessing.ct_nonvisualdrawingprops docpr = inline.addnewdocpr(); docpr.id = 1; docpr.name = "image" + id;}
今天先到这了,以后有收获再补充。
相关推荐:
使用c/c++编写php extension
【c#教程】c# 数据类型
以上就是c# 使用npoi生成word文档(按照模板)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product