poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现
package poi.test;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.Random; import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; public class StyleTest2 {
public static void main(String[] args) throws Exception {
StyleTest2 t = new StyleTest2();
XWPFDocument doc = new XWPFDocument();
// 需关闭护眼色才能看到效果
//t.setDocumentbackground(doc, "FDE9D9");//设置页面背景色
t.testSetUnderLineStyle(doc);//设置下划线样式以及突出显示文本
t.addNewPage(doc, BreakType.PAGE);
t.testSetShdStyle(doc);//设置文字底纹
t.saveDocument(doc,"e:/"+ System.currentTimeMillis() + ".docx");
} public void testSetUnderLineStyle(XWPFDocument doc) {
String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0",
"7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1",
"B5E900", "FEF8B6" };
Random random = new Random();
// 这里为了方便测试写了数字,推荐写英文样式
for (int i = 1; i <= 18; i++) {
XWPFParagraph p = doc.createParagraph();
setParagraphFontInfoAndUnderLineStyle(p,
"本文是以poi3.9读写2010word、2010excel、2010ppt", "华文行楷", "000000","22",
false, false, false, true,
i,colors[Math.abs(random.nextInt(colors.length))], false, 0,null);
setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0",
true,"240", STLineSpacingRule.Enum.forString("auto"));
setParagraphAlignInfo(p, ParagraphAlignment.LEFT,TextAlignment.CENTER);
}
} public void testSetShdStyle(XWPFDocument doc) {
String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0",
"7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1",
"B5E900", "FEF8B6" };
Random random = new Random();
// 这里为了方便测试写了数字,推荐写英文样式
for (int i = 1; i <= 38; i++) {
XWPFParagraph p = doc.createParagraph();
setParagraphFontInfoAndUnderLineStyle(p,
"本文是以poi3.9读写2010word、2010excel、2010ppt", "华文行楷", "1D8C56","22",
false, false, false, false,
i, null, true, i,colors[Math.abs(random.nextInt(colors.length))]);
setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0",
true,"240", STLineSpacingRule.Enum.forString("auto"));
setParagraphAlignInfo(p, ParagraphAlignment.LEFT,TextAlignment.CENTER);
}
} //设定水平对齐方式、垂直对齐方式
public void setParagraphAlignInfo(XWPFParagraph p,
ParagraphAlignment pAlign, TextAlignment valign) {
p.setAlignment(pAlign);
p.setVerticalAlignment(valign);
} //三组数,分别设定 ★段前段后磅数★段前段后行数★间距★
public void setParagraphSpacingInfo(XWPFParagraph p, boolean isSpace,String before, String after,
boolean isPLine, String beforeLines,String afterLines,
boolean isLine, String line,STLineSpacingRule.Enum lineValue) {
CTPPr pPPr = null;
if (p.getCTP() != null) {
if (p.getCTP().getPPr() != null) {
pPPr = p.getCTP().getPPr();
} else {
pPPr = p.getCTP().addNewPPr();
}
}
/**
* CTSpacing设置段落
*/
CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing()
: pPPr.addNewSpacing();
if (isSpace) {
// 段前磅数
if (before != null) {
pSpacing.setBefore(new BigInteger(before));
}
// 段后磅数
if (after != null) {
pSpacing.setAfter(new BigInteger(after));
}
}
if (isPLine) {
// 段前行数
if (beforeLines != null) {
pSpacing.setBeforeLines(new BigInteger(beforeLines));
}
// 段后行数
if (afterLines != null) {
pSpacing.setAfterLines(new BigInteger(afterLines));
}
}
// 间距
if (isLine) {
if (line != null) {
pSpacing.setLine(new BigInteger(line));
}
if (lineValue != null) {
pSpacing.setLineRule(lineValue);
}
}
} @SuppressWarnings("deprecation")
public void setParagraphFontInfoAndUnderLineStyle(XWPFParagraph p,
String content, String fontFamily, String colorVal,String fontSize,
boolean isBlod, boolean isItalic,boolean isStrike, boolean isUnderLine,
int underLineStyle,String underLineColor, boolean isShd, int shdValue, String shdColor) {
XWPFRun pRun = null;
if (p.getRuns() != null && p.getRuns().size() > 0) {
pRun = p.getRuns().get(0);
} else {
pRun = p.createRun();
}
pRun.setText(content);
/**
* CTRPr设置页
*/
CTRPr pRpr = null;
if (pRun.getCTR() != null) {
pRpr = pRun.getCTR().getRPr();
if (pRpr == null) {
pRpr = pRun.getCTR().addNewRPr();
}
}
/**
* CTFonts设置字体
*/
// 设置字体
CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr
.addNewRFonts();
fonts.setAscii(fontFamily);//---只改变Ascii中的(字母和数字)
fonts.setEastAsia(fontFamily);//---只改变中文EastAsia
fonts.setHAnsi(fontFamily);//--- /**
* CTHpsMeasure设置大小
*/
// 设置字体大小
CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();
sz.setVal(new BigInteger(fontSize)); CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr
.addNewSzCs();
szCs.setVal(new BigInteger(fontSize));//---字体大小 // 设置字体样式
if (isBlod) {
pRun.setBold(isBlod);//---是否加黑加粗
}
if (isItalic) {
pRun.setItalic(isItalic);//---是否倾斜
}
if (isStrike) {
pRun.setStrike(isStrike);//是否有中划线
}
if (colorVal != null) {
pRun.setColor(colorVal);//---字体颜色1D8C56
} // // 设置字突出显示文本---设置的文字的背景颜色,太难看了!!
// if (underLineStyle > 0 && underLineStyle < 17) {
// CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr
// .getHighlight() : pRpr.addNewHighlight();
// hightLight.setVal(STHighlightColor.Enum.forInt(underLineStyle));
// }
//
// 设置下划线样式
if (isUnderLine) {
CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();
u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));
if (underLineColor != null) {
u.setColor(underLineColor);
}
}
/**
* CTShd设置底纹
*/
if (isShd) {
// 设置底纹
CTShd shd = pRpr.isSetShd() ? pRpr.getShd() : pRpr.addNewShd();
if (shdValue > 0 && shdValue <= 38) {
shd.setVal(STShd.Enum.forInt(underLineStyle));
}
if (shdColor != null) {
shd.setColor(shdColor);
}
}
} // // 设置页面背景色
// public void setDocumentbackground(XWPFDocument document, String bgColor) {
// CTBackground bg = null;
// if( document.getDocument().isSetBackground()){
// bg = document.getDocument().getBackground();
// }else{
// bg = document.getDocument().addNewBackground();
// }
// bg.setColor(bgColor);
// } public void addNewPage(XWPFDocument document, BreakType breakType) {
XWPFParagraph xp = document.createParagraph();
xp.createRun().addBreak(breakType);
} public void saveDocument(XWPFDocument document, String savePath)
throws Exception {
FileOutputStream fos = new FileOutputStream(savePath);
document.write(fos);
fos.close();
}
}
poi-对于word的操作(二)的更多相关文章
- poi对word的操作(总结)
★★★ POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument. 1.正文段落:一个文档包含多个段落Paragraph,一个段落包含多个Runs,一个R ...
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Java POI 解析word文档
实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...
- POI生成word文档完整案例及讲解
一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...
- Java利用poi生成word(包含插入图片,动态表格,行合并)
转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...
- POI读写Word docx文件
使用POI读写word docx文件 目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx ...
- POI 读取word (word 2003 和 word 2007) (转)
最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...
- 使用POI读写Word doc文件
使用POI读写word doc文件 目录 1 读word doc文件 1.1 通过WordExtractor读文件 1.2 通过HWPFDocument读文件 2 写w ...
- android使用POI读写word doc文件
目录 1 读word doc文件 1.1 通过WordExtractor读文件 1.2 通过HWPFDocument读文件 2 写word doc文件 Apache p ...
随机推荐
- SpringBoot日志配置(详解) 涉及控制台输出日志、生成日志文件、日志级别修改、hibernate日志不输出
写在前面 本篇主要讲述日志配置,看完本篇可以解决下述问题, 控制台输出日志.生成日志文件.日志级别修改.hibernate日志不输出 Git Demo Path:https://github.com/ ...
- 拓扑排序(Toposort)
摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...
- 软工2017第四周作业结对编程——个人psp
29.22 --9.26本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 预计时间 开始时间 结束时间 中断时间 ...
- 软件工程 作业part2 采访
Part 2 采访本课程往届同学(含外校和毕业生). 现代软件工程这门课已经上了好几年了,以前有很多学生做过团队项目(说不定包括本校的学生),请你们找一个以前的团队采访一下. 我采访的是2016级于淼 ...
- Java学习个人备忘录之抽象类
抽象类 特点:1. 方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰,抽象方法必须定义在抽象类中.该类必须也被abstract修饰2. 抽象类不可以被实例化. 为什么? 因为调 ...
- Spring学习(二)—— java的动态代理机制
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- iOS开发解决页面滑动返回跟scrollView左右划冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...
- iOS开发UIApplication用法
1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplication对象,而且是单例的,如果 ...
- iOS-AFNetworking与ASIHTTPRequest的区别
一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession 2.ASI的底层实现基于纯C语言的CFNetwork框架 3.因为NSURLConnectio ...
- OSG学习:矩阵变换节点示例
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...