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 ...
随机推荐
- Keil ARM-CM3 printf输出调试信息到Debug (printf) Viewer
参考资料:http://www.keil.com/support/man/docs/jlink/jlink_trace_itm_viewer.htm 1.Target Options -> De ...
- HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
Problem Description One day, a hunter named James went to a mysterious area to find the treasures. J ...
- hosts_allow配置了却不生效
hosts_allow配置了却不生效 配置了两台白名单的机器,一台生效一台不生效,google后的结果都是更新libwrap.so 安装openssh等等..(问题还是没有解决) 经过对比发现,原来 ...
- HBase 参考文档翻译之 Getting Started
本篇是对HBase官方参考文档的大体翻译,介于本人英文水平实在有限,难免有纰漏之处.本篇不只是对官方文档的翻译,还加入了一些本人对HBase的理解.在翻译过程中,一些没有营养的废话,我就忽略了没有翻译 ...
- JSON解析与序列化
JSON之所以流行,拥有与JavaScript类似的语法并不是全部原因.更重要的一个原因是,可以把JSON数据结构解析为有用的 JavaScript对象.与XML数据结构要解析成DOM文档而且从中提取 ...
- LintCode-61.搜索区间
搜索区间 给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置. 如果目标值不在数组中,则返回[-1, -1] 样例 给出[5, 7, 7, 8, 8, 10]和目标值t ...
- BAT批处理(三)
1.set set命令:显示.设置或删除变量.显示变量:set 或 set s 前者显示批处理当前已定义的所有变量及其值,后者显示所有以s开头的变量及值.设置变量:set aa=abcd 此句命令便可 ...
- C#里面Console.Write()和Console.WriteLine()有什么区别?
Console.Write()和Console.WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显示出来.两着间的差异在Consol ...
- [剑指Offer] 48.不用加减乘除做加法
题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. [思路] 首先看十进制是如何做的: 5+7=12,三步走第一步:相加各位的值,不算进位,得到2.第二步:计算进 ...
- [BinaryTree] 二叉树类的实现
二叉树结点的抽象数据类型: template<class T> class BinaryTreeNode { friend class BinaryTree<T>; priva ...