在开发中有时候我们需要导出ms word文档。最近因为需要做一个生成word文件的功能。就将这块拿出来和大家分享。
生成word文件和我们写word文档是相同的概念,只不过在这里我们换成了用代码来操作。下面的例子中主要有添加页眉,页脚,正文(段落,表格)。在正文中,段落包含文字字体和背景的设置。表格主要是数据的填充和样式(有无边框)。这里写的例子给出的内容只是java poi 方式生成word文件的极少数的一些方法,需要使用更多方法的还是要自己根据自己的需求去查看api。
看到很多小伙伴反应不能用的问题,这里我又重新把代码下载下来生成了一次试试。确实是没有问题。以前使用的是jdk6,最后一个版本使用的是jdk8.我再把我的maven导包情况贴出来。供大家参考,生成的文件ms office 和wps打开均没有问题。
<dependency>
<groupid>org.apache.poi</groupid>
<artifactid>ooxml-schemas</artifactid>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core -->
<dependency>
<groupid>fr.opensagres.xdocreport</groupid>
<artifactid>org.apache.poi.xwpf.converter.core</artifactid>
<version>1.0.6</version>
</dependency>那就直接先上代码吧:
package com.seawater.controller;
import org.apache.poi.xwpf.model.xwpfheaderfooterpolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.file;
import java.io.fileoutputstream;
import java.math.biginteger;
/**
* created by zhouhs on 2017/1/9.
*/
public class wordexportcontroller {
public static void main(string[] args)throws exception {
//blank document
xwpfdocument document= new xwpfdocument();
//write the document in file system
fileoutputstream out = new fileoutputstream(new file("create_table.docx"));
//添加标题
xwpfparagraph titleparagraph = document.createparagraph();
//设置段落居中
titleparagraph.setalignment(paragraphalignment.center);
xwpfrun titleparagraphrun = titleparagraph.createrun();
titleparagraphrun.settext("java poi");
titleparagraphrun.setcolor("000000");
titleparagraphrun.setfontsize(20);
//段落
xwpfparagraph firstparagraph = document.createparagraph();
xwpfrun run = firstparagraph.createrun();
run.settext("java poi 生成word文件。");
run.setcolor("696969");
run.setfontsize(16);
//设置段落背景颜色
ctshd ctshd = run.getctr().addnewrpr().addnewshd();
ctshd.setval(stshd.clear);
ctshd.setfill("97ffff");
//换行
xwpfparagraph paragraph1 = document.createparagraph();
xwpfrun paragraphrun1 = paragraph1.createrun();
paragraphrun1.settext("\r");
//基本信息表格
xwpftable infotable = document.createtable();
//去表格边框
infotable.getcttbl().gettblpr().unsettblborders();
//列宽自动分割
cttblwidth infotablewidth = infotable.getcttbl().addnewtblpr().addnewtblw();
infotablewidth.settype(sttblwidth.dxa);
infotablewidth.setw(biginteger.valueof(9072));
//表格第一行
xwpftablerow infotablerowone = infotable.getrow(0);
infotablerowone.getcell(0).settext("职位");
infotablerowone.addnewtablecell().settext(": java 开发工程师");
//表格第二行
xwpftablerow infotablerowtwo = infotable.createrow();
infotablerowtwo.getcell(0).settext("姓名");
infotablerowtwo.getcell(1).settext(": seawater");
//表格第三行
xwpftablerow infotablerowthree = infotable.createrow();
infotablerowthree.getcell(0).settext("生日");
infotablerowthree.getcell(1).settext(": xxx-xx-xx");
//表格第四行
xwpftablerow infotablerowfour = infotable.createrow();
infotablerowfour.getcell(0).settext("性别");
infotablerowfour.getcell(1).settext(": 男");
//表格第五行
xwpftablerow infotablerowfive = infotable.createrow();
infotablerowfive.getcell(0).settext("现居地");
infotablerowfive.getcell(1).settext(": xx");
//两个表格之间加个换行
xwpfparagraph paragraph = document.createparagraph();
xwpfrun paragraphrun = paragraph.createrun();
paragraphrun.settext("\r");
//工作经历表格
xwpftable comtable = document.createtable();
//列宽自动分割
cttblwidth comtablewidth = comtable.getcttbl().addnewtblpr().addnewtblw();
comtablewidth.settype(sttblwidth.dxa);
comtablewidth.setw(biginteger.valueof(9072));
//表格第一行
xwpftablerow comtablerowone = comtable.getrow(0);
comtablerowone.getcell(0).settext("开始时间");
comtablerowone.addnewtablecell().settext("结束时间");
comtablerowone.addnewtablecell().settext("公司名称");
comtablerowone.addnewtablecell().settext("title");
//表格第二行
xwpftablerow comtablerowtwo = comtable.createrow();
comtablerowtwo.getcell(0).settext("2016-09-06");
comtablerowtwo.getcell(1).settext("至今");
comtablerowtwo.getcell(2).settext("seawater");
comtablerowtwo.getcell(3).settext("java开发工程师");
//表格第三行
xwpftablerow comtablerowthree = comtable.createrow();
comtablerowthree.getcell(0).settext("2016-09-06");
comtablerowthree.getcell(1).settext("至今");
comtablerowthree.getcell(2).settext("seawater");
comtablerowthree.getcell(3).settext("java开发工程师");
ctsectpr sectpr = document.getdocument().getbody().addnewsectpr();
xwpfheaderfooterpolicy policy = new xwpfheaderfooterpolicy(document, sectpr);
//添加页眉
ctp ctpheader = ctp.factory.newinstance();
ctr ctrheader = ctpheader.addnewr();
cttext ctheader = ctrheader.addnewt();
string headertext = "java poi create ms word file.";
ctheader.setstringvalue(headertext);
xwpfparagraph headerparagraph = new xwpfparagraph(ctpheader, document);
//设置为右对齐
headerparagraph.setalignment(paragraphalignment.right);
xwpfparagraph[] parsheader = new xwpfparagraph[1];
parsheader[0] = headerparagraph;
policy.createheader(xwpfheaderfooterpolicy.default, parsheader);
//添加页脚
ctp ctpfooter = ctp.factory.newinstance();
ctr ctrfooter = ctpfooter.addnewr();
cttext ctfooter = ctrfooter.addnewt();
string footertext = "http://blog.csdn.net/zhouseawater";
ctfooter.setstringvalue(footertext);
xwpfparagraph footerparagraph = new xwpfparagraph(ctpfooter, document);
headerparagraph.setalignment(paragraphalignment.center);
xwpfparagraph[] parsfooter = new xwpfparagraph[1];
parsfooter[0] = footerparagraph;
policy.createfooter(xwpfheaderfooterpolicy.default, parsfooter);
document.write(out);
out.close();
system.out.println("create_table document written success.");
}
}代码我放到这一个文件当中了。下面我就一些代码做一些解释,因为有的是我在做的过程中遇到的问题。大部分的代码大家都是一眼就可以看懂的。
//设置段落背景颜色
ctshd ctshd = run.getctr().addnewrpr().addnewshd();
ctshd.setval(stshd.clear);
ctshd.setfill("97ffff");这段代码设置段落的背景颜色。
如果我们的表格不需要边框呢就加下面的代码:
infotable.getcttbl().gettblpr().unsettblborders();
infotable换成自己的table名称就可以了。
建立一个表格的时候设置列宽跟随内容伸缩
cttblwidth infotablewidth = infotable.getcttbl().addnewtblpr().addnewtblw();
infotablewidth.settype(sttblwidth.dxa);
infotablewidth.setw(biginteger.valueof(9072));其他的代码我就不解释了。运行就可以得到我们的word文件了。
结果:

总结
到此这篇关于java使用poi生成word文档的文章就介绍到这了,更多相关java poi生成word文档内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论