时间:2020-10-16 12:57:02 | 栏目:JAVA代码 | 点击:次
Apache封装的POI组件对Excel,Wold的操作已经非常的丰富了,在项目上也会经常用到一些POI的基本操作
这里就简单的阐述POI操作Wold的基本工具类,代码还是有点粗造的,但是不影响使用。
这个类包含了一些对文本进行换行,加粗,倾斜,字体颜色,大小,首行缩进,添加边框等方法。分享给大家学习下:
下面就献上代码:
首先我们引入需要的第三方jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency>
工具类具体代码:
package com.herbert.test; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by Herbert on 2019/1/22. */ public class WoldUtil { private static XWPFDocument document = null; private static XWPFParagraph paragraph = null; /** * 初始化创建Word文件 */ public WoldUtil(){ document = new XWPFDocument(); } /** * 创建一个段落 * @param position 段落位置 * 0:居左 * 1:居中 * 2:居右 */ public void createParagraph(Integer position){ paragraph = document.createParagraph(); switch (position){ case 0: paragraph.setAlignment(ParagraphAlignment.LEFT); break; case 1: paragraph.setAlignment(ParagraphAlignment.CENTER); break; case 2: paragraph.setAlignment(ParagraphAlignment.RIGHT); break; default: paragraph.setAlignment(ParagraphAlignment.LEFT); break; } } /** * 单线边框 */ public void createBorder(){ paragraph.setBorderTop(Borders.THICK);//设置上边框 paragraph.setBorderBottom(Borders.THICK);//设置下边框 paragraph.setBorderLeft(Borders.THICK);//设置左边框 paragraph.setBorderRight(Borders.THICK);//设置右边框 } /** * 双线边框 */ public void createBorderDouble(){ paragraph.setBorderTop(Borders.DOUBLE);//设置上边框 paragraph.setBorderBottom(Borders.DOUBLE);//设置下边框 paragraph.setBorderLeft(Borders.DOUBLE);//设置左边框 paragraph.setBorderRight(Borders.DOUBLE);//设置右边框 } /** * 首行缩进 * @param indentation */ public void addTextIndent(Integer indentation){ paragraph.setIndentationFirstLine(indentation); } /** * 创建文本信息 * @param text 文本信息 * @param bold 是否加粗 true为加粗 * @param italic 是否倾斜 true为倾斜 * @param color 颜色码 * @param fontSize 字体大小 * @param fontFamily 设置字体 */ public void createRun(String text, Boolean bold,Boolean italic, String color,Integer fontSize,String fontFamily){ XWPFRun r = paragraph.createRun();//创建段落文本 r.setText(text); r.setBold(bold);//设置为粗体 true 为粗体 r.setItalic(italic);//设置为倾斜 true 为粗体 r.setColor(color);//设置颜色 r.setFontSize(fontSize); CTRPr rpr = r.getCTR().isSetRPr() ? r.getCTR().getRPr() : r.getCTR().addNewRPr(); CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii(fontFamily); fonts.setEastAsia(fontFamily); fonts.setHAnsi(fontFamily); } /** * 写到磁盘 * @param path */ public void write(String path){ try{ FileOutputStream out = new FileOutputStream(path); document.write(out); out.close(); }catch (IOException e){ e.printStackTrace(); System.out.println("文件出现错误"); } } /** * 创建空行 * @param counts 空行个数 */ public void createEmpty(Integer counts){ for(int i =0;i<counts;i++){ XWPFParagraph empty = document.createParagraph(); XWPFRun e = empty.createRun(); e.setText(" "); } } /** * 查询wold中的数据 返回List 集合 * @param path wold所在的地址 * @return */ public List<String> query(String path){ List<String> list = new ArrayList<String>(); try{ FileInputStream stream = new FileInputStream(path); XWPFDocument doc = new XWPFDocument(stream);// 创建Word文件 for(XWPFParagraph p : doc.getParagraphs()){//遍历段落 System.out.println(p.getParagraphText()); list.add(p.getParagraphText()); } }catch (Exception e){ e.printStackTrace(); } return list; } public static void main(String args[]) throws Exception { WoldUtil woldUtil = new WoldUtil(); woldUtil.createParagraph(1); woldUtil.createBorder(); woldUtil.createRun("(标题居中)Apache POI的组件", true, false,"000000",17,FontFamily.BLACKBODY); woldUtil.createBorderDouble(); woldUtil.createParagraph(0); woldUtil.createRun("(边框)Apache POI包含用于处理MS-Office的所有OLE2复合文档的类和方法。该API的组件列表如下:", false,true, "4682B4",15,FontFamily.CHINESE_FINE_BLACK); woldUtil.createEmpty(3); woldUtil.createParagraph(0); woldUtil.addTextIndent(600); woldUtil.createRun("(首行缩进)• POIFS(不良混淆实现文件系统) - 此组件是所有其他POI元素的基本因素。它用于显式读取不同的文件", true,false,"A0522D",13,FontFamily.MICROSOFT_YAHEI); woldUtil.createEmpty(1); woldUtil.createParagraph(0); woldUtil.createRun("•HWPF(可怕的字处理器格式) - 用于读写MS-Word的.doc扩展文件。", true,false,"eeff00",13,FontFamily.MICROSOFT_YAHEI); woldUtil.write("E:\\herbert\\sample.doc"); List<String> list =woldUtil.query("E:\\herbert\\sample.doc"); System.out.println("list=====>>>>>>>> "+list.toString()); } }
测试截图