直接上代码:
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.CTBackground; 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 POI_07_设置下划线样式_S3_Test { public static void main(String[] args) throws Exception { POI_07_设置下划线样式_S3_Test t = new POI_07_设置下划线样式_S3_Test(); XWPFDocument doc = new XWPFDocument(); // 需关闭护眼色才能看到效果 t.setDocumentbackground(doc, "FDE9D9");//设置页面背景色 t.testSetUnderLineStyle(doc);//设置下划线样式以及突出显示文本 t.addNewPage(doc, BreakType.PAGE); t.testSetShdStyle(doc);//设置文字底纹 t.saveDocument(doc, "f:/saveFile/temp/sys_" + 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(); // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 18; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphFontInfoAndUnderLineStyle(p, "测试下划线", "宋体", "1D8C56", "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(); // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 38; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphFontInfoAndUnderLineStyle(p, "测试底纹", "宋体", "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 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); } } } 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 pRpr = null; if (pRun.getCTR() != null) { pRpr = pRun.getCTR().getRPr(); if (pRpr == null) { pRpr = pRun.getCTR().addNewRPr(); } } // 设置字体 CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr .addNewRFonts(); fonts.setAscii(fontFamily); fonts.setEastAsia(fontFamily); fonts.setHAnsi(fontFamily); // 设置字体大小 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); } // 设置字突出显示文本 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); } } if (isShd) { // 设置底纹 CTShd shd = pRpr.isSetShd() ? pRpr.getShd() : pRpr.addNewShd(); if (shdValue > 0 && shdValue <= 38) { shd.setVal(STShd.Enum.forInt(shdValue)); } if (shdColor != null) { shd.setColor(shdColor); } } } // 设置页面背景色 public void setDocumentbackground(XWPFDocument document, String bgColor) { CTBackground bg = document.getDocument().isSetBackground() ? document .getDocument().getBackground() : 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(); } }
结果为:
转载请注明原处,原文链接:http://53873039oycg.iteye.com/blog/2157758 ,谢谢。
全文完。
相关推荐
POI解析word2007文本及图片(已测试).doc
java poi 实现word文本的替换,支持doc和docx格式的文本替换
POI对Word的操作,特别是针对生成报告模板时,向word中写入文本和图片。
POI 替换word2007文本框的值POI,包含word表中的文本框的值,定义好word模板即可替换word中的值。
POI导出Word包含页眉 页脚的设置,标题的设置以及显示显示设置,包括POI操作Word完整的jar包
资源描述了poi对于word的操作,poi合并word文档,poi设置word页码,内含maven依赖文档和完整代码,是一个完整工程,打开即可用
这是一个POI读取word 2003 和 word 2007的例子 是一个Eclipse工程 下载后直接导入工程 运行 src 目录下的 Test java 类即可 这个 rar 包中包含着 POI 读取word 2003 和 word 2007 所需要的 jar 包 也有需要读取的 ...
利用POI将word转换成html实现在线阅读
poi将word里面的内容转换成html、包括word里面字体的大小、表格的处理、还有图片、段落等等...
主要介绍了java poi设置生成的word的图片为上下型环绕以及其位置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
使用poi替换word中的文字和图片实现打印,注意代码中的jar包、图片、word位置
NULL 博文链接:https://53873039oycg.iteye.com/blog/2187388
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。 test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
POI报表Word导出
POI操作Word需要的jar包 博文链接:https://lxzqz.iteye.com/blog/2428443
使用poi将word读取后替换指定内容后再次生成新word,本人经过测试,拿来即用!
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
完美市县了DOCX格式的模板文字替换与插入浮于文字上方的图片。