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的操作(二)的更多相关文章

  1. poi对word的操作(总结)

    ★★★ POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.    1.正文段落:一个文档包含多个段落Paragraph,一个段落包含多个Runs,一个R ...

  2. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  3. Java POI 解析word文档

    实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...

  4. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  5. Java利用poi生成word(包含插入图片,动态表格,行合并)

    转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...

  6. POI读写Word docx文件

    使用POI读写word docx文件 目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx ...

  7. POI 读取word (word 2003 和 word 2007) (转)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...

  8. 使用POI读写Word doc文件

    使用POI读写word doc文件 目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写w ...

  9. android使用POI读写word doc文件

    目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写word doc文件 Apache p ...

随机推荐

  1. php面试的那些“黑话”

    以下是一些常见的面试暗语,求职者一定要弄清楚其中蕴含的深意,不然可能“躺着也中枪”,最后只能铩羽而归. (1)请把简历先放在这,有消息我们会通知你的 面试官说出这句话,则表明他对你已经“兴趣不大”,为 ...

  2. 小程序之web-view打开外部链接

    小程序之web-view - 传送门 web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面.个人类型与海外类型的小程序暂不支持使用. 一:小程序使用web-view打开链接的前 ...

  3. Python学习之路1 - 基础入门

    本文内容 Python介绍 安装Python解释器 输出 变量 输入 条件判断语句 循环语句 模块讲解 三元运算 字符串和二进制的相互转化 本系列文章使用的Python版本为3.6.2 使用开发工具为 ...

  4. 关于GenericJDBCException的问题

    在spring和hibernate整合的初步阶段,还没有编辑hibernate.cfg.xml这个文件,只有一个beans.xml文件.此时遇到了一个bug. Exception in thread ...

  5. Ubuntu 12.04.1 LTS 升级 PHP 从5.3 到 5.5

    #!/bin/bash # desc install php5.5 #add-apt-repository ppa:ondrej/php5 #apt-get install python-softwa ...

  6. 常见设备在linux中的文件名

    设备 linux中的文件名 IDE硬盘 /dev/hd[a-d] SATA/USB/SCSI/SAS /dev/sd[a-p] 软盘 /dev/fd[0-1] 打印机 25针:/dev/lp[0-2] ...

  7. linux核心版本号的说明

    日志不会很长,因为每天都在学习,我认为的重点,我自己做的记录,我很高兴能分享给大家: Linux的核心版本编号有点类似如下癿样子: 2.6.18-92.el5 主版本.次版本.释出版本-修改版本 因为 ...

  8. MongoDB、ElasticSearch、Redis、HBase这四种热门数据库的优缺点及应用场景

    MongoDB MongoDB是当今最火爆的NoSQL数据库.MongoDB最早在09年发布,算得上是早期大数据时代的数据库代表作了.随着MongoDB的火爆,研发MongoDB的团队还专门成立了Mo ...

  9. DELPHI的MEMO组件

    位于Standard选项卡上,它是对EDIT控件的扩展,可以对多行文本进行显示.输入 和编辑. Lines属性: 该属性实际上为TStrings类型的对象,用来存放Memo对象的文本 TStrings ...

  10. 2018 杭电多校3 - M.Walking Plan

    题目链接 Problem Description There are $$$n$$$ intersections in Bytetown, connected with $$$m$$$ one way ...