1.需要引入jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.17</version> </dependency> <!-- docx生成html--> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.core</artifactId> <version>1.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.xhtml --> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency>
2.对应代码
package com.sdyy.pd.duplicatecheck.common.utils.file; /** * @Title word文档读取处理 * @Desc * @Date 2020/5/19 * @Author liuch **/ import com.sdyy.pd.duplicatecheck.common.utils.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.*; import java.math.BigInteger; import java.util.HashMap; import java.util.List; import java.util.Map; public class WordUtils { private static Map<String, Map<String, Object>> orderMap = new HashMap<String, Map<String, Object>>(); // 日志记录器 public final Log logger = LogFactory.getLog(this.getClass()); public void init(String targetPath, String sourcePath) { InputStream is = null; XWPFDocument doc = null; OutputStream out = null; try { XWPFDocument createDoc = new XWPFDocument(); is = new FileInputStream(sourcePath); doc = new XWPFDocument(is); //获取段落 List<XWPFParagraph> paras = doc.getParagraphs(); for (XWPFParagraph para : paras) { // System.out.println(para.getCTP());//得到xml格式 System.out.println(para.getStyleID());//段落级别 System.out.println(para.getParagraphText());//段落内容 String titleLvl = getTitleLvl(doc, para);//获取段落级别 if ("a5".equals(titleLvl) || "HTML".equals(titleLvl) || "".equals(titleLvl) || null == titleLvl) { titleLvl = "8"; } System.out.println(titleLvl + "-----");//0,1,2 if (!"8".equals(titleLvl)) { System.out.println(titleLvl + "====" + para.getParagraphText()); } XWPFParagraph ctPara = createDoc.createParagraph(); //一个XWPFRun代表具有相同属性的一个区域。 XWPFRun ctRun = ctPara.createRun(); String ctText = para.getParagraphText(); ctRun.setFontFamily("宋体");//字体 ctRun.setFontSize(12); if (null != titleLvl && !"".equals(titleLvl) && !"8".equals(titleLvl)) {