直接上代码:
import java.io.FileOutputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; 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.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; 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.CTJc; 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.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; 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.testSetParagraphUnderLineStyle(doc); t.addNewPage(doc, BreakType.PAGE); // 测试文本高亮 t.testSetParagraphTextHightLightStyle(doc); t.addNewPage(doc, BreakType.PAGE); // 测试字符底纹 t.testSetParagraphTextShdStyle(doc); // 测试段落底纹 t.testSetParagraphShdStyle(doc); // 测试表格样式 t.testTableStyle(doc); t.saveDocument(doc, "f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx"); } public void testSetParagraphUnderLineStyle(XWPFDocument doc) { String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", "A6DBFF", "FEF8B6" }; Random random = new Random(); // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 18; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphTextStyleInfo(p, "测试下划线_" + i, "宋体", "1D8C56", "22", false, false, false, true, i, colors[Math.abs(random.nextInt(colors.length))], false, 0); setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", true, "240", STLineSpacingRule.Enum.forString("auto")); setParagraphAlignInfo(p, ParagraphAlignment.LEFT, TextAlignment.CENTER); } } public void testSetParagraphTextHightLightStyle(XWPFDocument doc) { // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 17; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphTextStyleInfo(p, "测试文本高亮_" + i, "宋体", "1D8C56", "22", false, false, false, false, 0, null, true, i); setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", true, "240", STLineSpacingRule.Enum.forString("auto")); setParagraphAlignInfo(p, ParagraphAlignment.LEFT, TextAlignment.CENTER); } } public void testSetParagraphTextShdStyle(XWPFDocument doc) { String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", "A6DBFF", "FEF8B6" }; Random random = new Random(); // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 38; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphTextStyleInfo(p, "测试文本底纹_" + i, "宋体", "1D8C56", "22", false, false, false, false, 0, null, false, 0); setParagraphTextShdStyle(p, 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 testSetParagraphShdStyle(XWPFDocument doc) { String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", "A6DBFF", "FEF8B6" }; Random random = new Random(); // TODO 这里为了方便测试写了数字,推荐写英文样式 for (int i = 1; i <= 38; i++) { XWPFParagraph p = doc.createParagraph(); setParagraphTextStyleInfo(p, "测试段落底纹_" + i, "宋体", "1D8C56", "22", false, false, false, false, 0, null, false, 0); setParagraphShdStyle(p, 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.CENTER, 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 setParagraphTextStyleInfo(XWPFParagraph p, String content, String fontFamily, String colorVal, String fontSize, boolean isBlod, boolean isItalic, boolean isStrike, boolean isUnderLine, int underLineStyle, String underLineColor, boolean isHightLight, int hightLightValue) { 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 (colorVal != null) { pRun.setColor(colorVal); } // 设置字体样式 if (isBlod) { pRun.setBold(isBlod); } if (isItalic) { pRun.setItalic(isItalic); } if (isStrike) { pRun.setStrike(isStrike); } if (colorVal != null) { pRun.setColor(colorVal); } // 设置下划线样式 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 (isHightLight) { if (hightLightValue > 0 && hightLightValue < 17) { CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr .getHighlight() : pRpr.addNewHighlight(); hightLight .setVal(STHighlightColor.Enum.forInt(hightLightValue)); } } } public void setParagraphShdStyle(XWPFParagraph p, boolean isShd, int shdValue, String shdColor) { XWPFRun pRun = null; if (p.getRuns() != null && p.getRuns().size() > 0) { pRun = p.getRuns().get(0); } if (pRun == null) { return; } CTPPr pPpr = null; if (pRun.getCTR() != null) { pPpr = p.getCTP().getPPr(); if (pPpr == null) { pPpr = p.getCTP().addNewPPr(); } } CTShd shd = pPpr.isSetShd() ? pPpr.getShd() : pPpr.addNewShd(); if (shdValue > 0 && shdValue <= 38) { shd.setVal(STShd.Enum.forInt(shdValue)); } if (shdColor != null) { shd.setColor(shdColor); // shd.setFill(shdColor); // shd.setColor("auto"); } } public void setParagraphTextShdStyle(XWPFParagraph p, boolean isShd, int shdValue, String shdColor) { XWPFRun pRun = null; if (p.getRuns() != null && p.getRuns().size() > 0) { pRun = p.getRuns().get(0); } if (pRun == null) { return; } CTRPr pRpr = null; if (pRun.getCTR() != null) { pRpr = pRun.getCTR().getRPr(); if (pRpr == null) { pRpr = pRun.getCTR().addNewRPr(); } } 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); // shd.setColor("auto"); // shd.setFill(shdColor); } } } public void testTableStyle(XWPFDocument doc) { String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", "A6DBFF", "FEF8B6" }; Random random = new Random(); List<String> columnList = new ArrayList<String>(); columnList.add("序号"); columnList.add("姓名信息|姓甚|名谁"); columnList.add("名刺信息|籍贯|营生"); columnList.add("字"); XWPFTable table = doc.createTable(7, 6); setTableWidth(table, "8000"); XWPFTableRow firstRow = table.getRow(0); XWPFTableRow secondRow = table.getRow(1); firstRow.setHeight(400); secondRow.setHeight(400); XWPFTableCell firstCell = null; XWPFTableCell secondCell = null; int firstCellIndex = 0; for (String str : columnList) { if (str.indexOf("|") == -1) { firstCell = firstRow.getCell(firstCellIndex); setCellText(firstCell, str, 1600, true, 6, "CCCCCC"); firstCellIndex++; } else { String[] strArr = str.split("\\|"); for (int i = 1; i < strArr.length; i++) { firstCell = firstRow.getCell(firstCellIndex); setCellText(firstCell, strArr[0], 1600, true, 6, "CCCCCC"); secondCell = secondRow.getCell(firstCellIndex); setCellText(secondCell, strArr[i], 1600, true, 6, "CCCCCC"); firstCellIndex++; } } } // 合并行(跨列) firstCellIndex = 0; for (String str : columnList) { if (str.indexOf("|") == -1) { firstCellIndex++; } else { String[] strArr = str.split("\\|"); mergeCellsHorizontal(table, 0, firstCellIndex, firstCellIndex + strArr.length - 2); firstCellIndex += strArr.length - 1; } } // 合并列(跨行) firstCellIndex = 0; for (String str : columnList) { if (str.indexOf("|") == -1) { mergeCellsVertically(table, firstCellIndex, 0, 1); firstCellIndex++; } else { String[] strArr = str.split("\\|"); firstCellIndex += strArr.length - 1; } } int k = 0; // 数据 for (int i = 2; i < 7; i++) { firstRow = table.getRow(i); firstRow.setHeight(380); for (int j = 0; j < 6; j++) { firstCell = firstRow.getCell(j); setCellText(firstCell, "测试", 1600, true, k % 38, colors[Math.abs(random.nextInt(colors.length))]); k++; } } } public void setCellText(XWPFTableCell cell, String text, int width, boolean isShd, int shdValue, String shdColor) { CTTc cttc = cell.getCTTc(); CTTcPr ctPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr(); CTShd ctshd = ctPr.isSetShd() ? ctPr.getShd() : ctPr.addNewShd(); ctPr.addNewTcW().setW(BigInteger.valueOf(width)); if (isShd) { if (shdValue > 0 && shdValue <= 38) { ctshd.setVal(STShd.Enum.forInt(shdValue)); } if (shdColor != null) { // ctshd.setFill(shdColor); // ctshd.setColor("auto"); ctshd.setColor(shdColor); } } ctPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cell.setText(text); } /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) { for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) { XWPFTableCell cell = table.getRow(row).getCell(cellIndex); if (cellIndex == fromCell) { cell.getCTTc().addNewTcPr().addNewHMerge() .setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewHMerge() .setVal(STMerge.CONTINUE); } } } /** * @Description: 跨行合并 */ public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { XWPFTableCell cell = table.getRow(rowIndex).getCell(col); if (rowIndex == fromRow) { cell.getCTTc().addNewTcPr().addNewVMerge() .setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewVMerge() .setVal(STMerge.CONTINUE); } } } public void setTableWidth(XWPFTable table, String width) { CTTbl ttbl = table.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl .getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr .addNewTblW(); CTJc cTJc = tblPr.addNewJc(); cTJc.setVal(STJc.Enum.forString("center")); tblWidth.setW(new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } // 设置页面背景色 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/2157923,谢谢。
全文完。
相关推荐
值得注意的是 POI 在读取 word 文件的时候不会读取 word 文件中的图片信息 还有就是对于 2007 版的 word docx 如果 word 文件中有表格 所有表格中的数据都会在读取出来的字符串的最后 ">这是一个POI读取word 2003 和...
标题中的“poi读取word 2007简单文本框值”指的是使用Apache POI库来读取Microsoft Word 2007文档中简单文本框内的文本内容。Apache POI是一个流行的开源Java库,用于处理Microsoft Office格式的文件,如Word(.docx...
2. **Word与HTML的结构差异** Word文档基于复杂的OOXML标准,包含各种元素如文本、样式、表格、图片等。HTML则是一种标记语言,主要用于网页布局和内容展示。将Word转换为HTML需要解析Word文档并将其内容重新构建为...
Apache POI 解析 Word 2007 文本及图片 Apache POI (Poor Obfuscation Implementation) 是一个 Java 库,用于读取和写入 Microsoft Office 文件,包括 Word、Excel、PowerPoint 等。 Apache POI 提供了对 Word ...
在这个例子中,我们将探讨如何使用POI库来处理Word文档,特别是利用模板文件循环输出表格行并导出新的Word文档。这个过程在数据分析、报告生成或自动化文档制作等场景中非常有用。 首先,我们需要理解Word文档的...
在本主题中,我们将深入探讨如何使用POI库来导出Word文档,并进行页眉、页脚和标题的设置。这个过程涉及到对Word文档的高级布局控制,对于自动化报告生成或批量创建文档非常有用。 1. **Apache POI简介** Apache ...
在本文中,我们将深入探讨如何使用POI库来替换Word2007文档中的文本框值,这对于自动化报告生成、批量文档编辑以及模板驱动的文档创建等场景非常有用。 首先,我们需要了解Word2007文档(.docx)的内部结构。它是...
Apache POI是一个强大的Java库,专门用于处理Microsoft Office格式的文件,如Word、Excel和PowerPoint。在本项目中,我们将关注如何使用Apache POI来根据预设的Word模板生成包含替换内容、循环列表和图片的动态Word...
本篇文章将深入探讨如何利用POI库在Word文档中进行模板替换以及循环插入表格的操作。 首先,我们需要理解Apache POI的工作原理。POI提供了HWPFOI(用于处理老版的.doc文件)和XWPF(用于处理新版的.docx文件)两个...
在本主题中,我们将聚焦于如何使用Apache POI来操作Word文档中的表格。 在Word文档中,表格是一种常用的数据展示和组织方式。使用Apache POI,我们可以创建、修改、读取和格式化Word文档中的表格。以下是一些关键...
POI报表Word导出
【Java POI 设置 Word 图片为上下型环绕及位置实现详解】 在使用 Java POI 库生成 Word 文档时,有时我们需要对插入的图片进行格式调整,比如设置为上下型环绕,使其在文本中占据独立空间,同时可以调整图片的位置...
在"poi合并多个word文档并设置页码"这个主题中,我们将深入探讨如何利用POI API来实现这两个功能。 首先,让我们讨论如何合并多个Word文档。Apache POI提供了一个名为`XWPFDocument`的类,它代表一个Open XML格式的...
对于Word文档,POI提供了HWPF(Horrible Word Processor Format)用于处理旧版的.doc文件,以及XWPF(XML Word Processor Format)用于处理较新的.docx文件。 1. **解析.doc文件**: 对于老版本的Word文档,如.doc...
在IT行业中,尤其是在文档自动化处理领域,Apache POI是一个非常重要的工具,它允许开发者使用Java来操作Microsoft Office格式的文件,如Word、Excel和PowerPoint。本教程将深入讲解如何利用Apache POI的XWPF模块...
绝对好用的word操作 poi word2010替换文字 图片 表格
本项目中的"poiTest"是一个Web应用示例,它演示了如何使用Apache POI来解析不同版本的Excel(如2007、2010)以及Word2007和2010文档。 1. **Apache POI 简介** Apache POI 是由Apache软件基金会开发的一个项目,...
在本项目中,“使用java Apache poi 根据word模板生成word报表.rar”是一个压缩包,其中包含了一套使用Java和Apache POI库来根据预设的Word模板生成定制化报表的解决方案。 首先,我们需要理解Apache POI API的工作...
通过本文介绍的技术点,我们可以轻松地创建具有标题、段落、表格以及图片的复杂 Word 文档。需要注意的是,本文提供的示例仅涵盖了 Java POI 的一部分功能,更多高级功能和细节还需要参考官方文档进行深入学习。 综...
POI无法读取到word每一段所在的页码。我给出了代码和所有的解释,用POI读取word中的页码。也算是小方法了,基本很准确,为什么要加一个基本呢?因为,分页标志符号在第一行为空白行的时候没有分页符!!所以你的文档...