xwpfdocument 保存修改_java使用POI操作XWPFDocument 生成Word实战(一)【比较详细的】...
注:我使用的word 2016
功能简介:
(1)使用jsoup解析html得到我用来生成word的文本(这个你们可以忽略)
(2)生成word、设置页边距、设置页脚(页码),设置页码(文本)
一、解析html
Document doc = Jsoup.parseBodyFragment(contents);
Element body = doc.body();
Elements es = body.getAllElements();
二、循环Elements获取我需要的html标签
boolean tag = false;
for (Element e : es) {
//跳过第一个(默认会把整个对象当做第一个)
if(!tag) {
tag = true;
continue;
}
//创建段落:生成word(核心)
createXWPFParagraph(docxDocument,e);
}
三、生成段落
/**
* 构建段落
* @param docxDocument
* @param e
*/
public static void createXWPFParagraph(XWPFDocument docxDocument, Element e){
XWPFParagraph paragraph = docxDocument.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(e.text());
run.setTextPosition(35);//设置行间距
if(e.tagName().equals("titlename")){
paragraph.setAlignment(ParagraphAlignment.CENTER);//对齐方式
run.setBold(true);//加粗
run.setColor("000000");//设置颜色--十六进制
run.setFontFamily("宋体");//字体
run.setFontSize(24);//字体大小
}else if(e.tagName().equals("h1")){
addCustomHeadingStyle(docxDocument, "标题 1", 1);
paragraph.setStyle("标题 1");
run.setBold(true);
run.setColor("000000");
run.setFontFamily("宋体");
run.setFontSize(20);
}else if(e.tagName().equals("h2")){
addCustomHeadingStyle(docxDocument, "标题 2", 2);
paragraph.setStyle("标题 2");
run.setBold(true);
run.setColor("000000");
run.setFontFamily("宋体");
run.setFontSize(18);
}else if(e.tagName().equals("h3")){
addCustomHeadingStyle(docxDocument, "标题 3", 3);
paragraph.setStyle("标题 3");
run.setBold(true);
run.setColor("000000");
run.setFontFamily("宋体");
run.setFontSize(16);
}else if(e.tagName().equals("p")){
//内容
paragraph.setAlignment(ParagraphAlignment.BOTH);//对齐方式
paragraph.setIndentationFirstLine(WordUtil.ONE_UNIT);//首行缩进:567==1厘米
run.setBold(false);
run.setColor("001A35");
run.setFontFamily("宋体");
run.setFontSize(14);
//run.addCarriageReturn();//回车键
}else if(e.tagName().equals("break")){
paragraph.setPageBreak(true);//段前分页(ctrl+enter)
}
}
四、设置页边距
/**
* 设置页边距 (word中1厘米约等于567)
* @param document
* @param left
* @param top
* @param right
* @param bottom
*/
public static void setDocumentMargin(XWPFDocument document, String left,String top, String right, String bottom) {
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar ctpagemar = sectPr.addNewPgMar();
if (StringUtils.isNotBlank(left)) {
ctpagemar.setLeft(new BigInteger(left));
}
if (StringUtils.isNotBlank(top)) {
ctpagemar.setTop(new BigInteger(top));
}
if (StringUtils.isNotBlank(right)) {
ctpagemar.setRight(new BigInteger(right));
}
if (StringUtils.isNotBlank(bottom)) {
ctpagemar.setBottom(new BigInteger(bottom));
}
}
五、创建页眉
/**
* 创建默认页眉
*
* @param docx XWPFDocument文档对象
* @param text 页眉文本
* @return 返回文档帮助类对象,可用于方法链调用
* @throws XmlException XML异常
* @throws IOException IO异常
* @throws InvalidFormatException 非法格式异常
* @throws FileNotFoundException 找不到文件异常
*/
public static void createDefaultHeader(final XWPFDocument docx, final String text){
CTP ctp = CTP.Factory.newInstance();
XWPFParagraph paragraph = new XWPFParagraph(ctp, docx);
ctp.addNewR().addNewT().setStringValue(text);
ctp.addNewR().addNewT().setSpace(SpaceAttribute.Space.PRESERVE);
CTSectPr sectPr = docx.getDocument().getBody().isSetSectPr() ? docx.getDocument().getBody().getSectPr() : docx.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
XWPFHeader header = policy.createHeader(STHdrFtr.DEFAULT, new XWPFParagraph[] { paragraph });
header.setXWPFDocument(docx);
}}
六、创建页脚
/**
* 创建默认的页脚(该页脚主要只居中显示页码)
*
* @param docx
* XWPFDocument文档对象
* @return 返回文档帮助类对象,可用于方法链调用
* @throws XmlException
* XML异常
* @throws IOException
* IO异常
*/
public static void createDefaultFooter(final XWPFDocument docx) {
// TODO 设置页码起始值
CTP pageNo = CTP.Factory.newInstance();
XWPFParagraph footer = new XWPFParagraph(pageNo, docx);
CTPPr begin = pageNo.addNewPPr();
begin.addNewPStyle().setVal(STYLE_FOOTER);
begin.addNewJc().setVal(STJc.CENTER);
pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
pageNo.addNewR().addNewInstrText().setStringValue("PAGE \\* MERGEFORMAT");
pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
CTR end = pageNo.addNewR();
CTRPr endRPr = end.addNewRPr();
endRPr.addNewNoProof();
endRPr.addNewLang().setVal(LANG_ZH_CN);
end.addNewFldChar().setFldCharType(STFldCharType.END);
CTSectPr sectPr = docx.getDocument().getBody().isSetSectPr() ? docx.getDocument().getBody().getSectPr() : docx.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
policy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[] { footer });
}
七、自定义标题样式(这个在我另一篇word基础中也有提及)
/**
* 增加自定义标题样式。这里用的是stackoverflow的源码
*
* @param docxDocument 目标文档
* @param strStyleId 样式名称
* @param headingLevel 样式级别
*/
private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
// lower number > style is more prominent in the formats bar
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// style shows up in the formats bar
ctStyle.setQFormat(onoffnull);
// style defines a heading of the given level
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
// is a null op if already defined
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
八、设置页码大小及纸张方向
/**
* 设置页面大小及纸张方向 landscape横向
* @param document
* @param width
* @param height
* @param stValue
*/
public void setDocumentSize(XWPFDocument document, String width,String height, STPageOrientation.Enum stValue) {
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
pgsz.setH(new BigInteger(height));
pgsz.setW(new BigInteger(width));
pgsz.setOrient(stValue);
}
九、效果展示
十、demo源码及生成的word文件(相应的jar包大家可以去阿里的maven仓库下载)
demo:https://pan.baidu.com/s/1jHFLniI
xwpfdocument 保存修改_java使用POI操作XWPFDocument 生成Word实战(一)【比较详细的】...相关推荐
- xwpfdocument 保存修改_java使用POI操作XWPFDocument 生成Word实战(一)
注:我使用的word 2016 功能简介: (1)使用jsoup解析html得到我用来生成word的文本(这个你们可以忽略) (2)生成word.设置页边距.设置页脚(页码),设置页码(文本) 一.解 ...
- poi处理word内容的公式_Java 使用 POI 操作 XWPFDocument 中的 XWPFParagraph(段落)对象的属性略解 Wor - 链滴...
我用的是office word 2016版 创建文本对象 XWPFDocument docxDocument = new XWPFDocument(); 创建段落对象 XWPFParagraph pa ...
- java使用poi操作world生成饼图,柱状图,折线图,组合图:二
java使用poi操作world生成饼图,柱状图,折线图,组合图:二 上文和问题链接 直接上代码 maven 测试类:单图表(入口) 测试类:组合图表(入口) 工具类:组合数据类 工具类:枚举解析图表 ...
- java使用poi操作world生成饼图,柱状图,折线图,组合图:一
java使用poi操作world生成饼图,柱状图,折线图,组合图:一 下文和问题链接 开发前准备 准备模板 模板下载地址百度云盘:cvod 本文可用操作 组合图操作 模板效果对比填充后的效果 饼图 模 ...
- 简单实现POI操作Excel生成数据透视图
简单实现POI操作Excel生成数据透视图 需求 思考 尝试 实现 搞完收工,去画模板了 需求 财务部门需要做一自动导出数据报表的功能,其中要有指定格式的数据透视图(柱状图.饼状图等等) 思考 正常来 ...
- Java使用POI通过模板生成Word
Java使用POI通过模板生成Word 前言 最近工作需要用到,所以记录下来以便查找. 一.概述 POI读写word使用的核心类是XWPFDocument.一个XWPFDocument代表一个docx ...
- 使用poi根据模版生成word文档并转换成PDF文件(可处理doc文件与docx文件版)
该篇文章是<使用poi根据模版生成word文档并转换成PDF文件>后续解决传入文件为doc文档或docx的处理方法 /*** 根据模板生成word** @param path 模板的路径* ...
- java中CTSectPr_java使用POI操作XWPFDocument 生成Word实战(一)
注:我使用的word 2016 功能简介: (1)使用jsoup解析html得到我用来生成word的文本(这个你们可以忽略) (2)生成word.设置页边距.设置页脚(页码),设置页码(文本) 一.解 ...
- 利用POI操作不同版本号word文档中的图片以及创建word文档
我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...
最新文章
- Python 常见的坑汇总
- html5控制单片机,10.2 单片机中 PWM 的原理与控制程序
- No resource found that matches the given name 'android:Widget.Material.A解决方案
- .net WebApi 开发中某些注意事项
- C++学习之路 | PTA乙级—— 1006 换个格式输出整数 (15分)(精简)
- ubuntu12.04 android studio 安装
- 还对Angel平台一知半解?腾讯专家为你指点迷津!
- 《疯狂Java讲义》(三十)---- Annotation
- 大型网站限流算法的实现和改造
- 手机steam未能连接到服务器1004,steam与服务器连接失败
- Flash游戏开发必备书籍
- Java Web(九) 用户管理系统
- Maya2018安装及激活教程
- 毛戈平上市以“光影美学”的理念打造适合中国人的高端化妆品品牌
- ICPC冠军教练亲自授课 字节跳动ICPC冬令营全球招募50支受训队
- STEAM 教育相关书籍
- 蚂蚁区块链BaaS平台架构与实践
- 2014最火的10分钟6块腹肌动图gif教程,男人不能没有腹肌!
- 201226元件_看电阻型号
- 如何打包谷歌浏览器Chrome的扩展程序