maven 依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3</version> </dependency>
1: docx格式转换
2:doc格式转换
以下方法没有转图片
/** * @param path 文件地址 * @param fileName 文件名 (word名) * @Description docx格式Word转html * @Throws * @Return void * @Date 2020-04-08 19:27:55 * @Author WangKun */ public static void docxConvertHtml(String path, String fileName) throws IOException { InputStream input = new FileInputStream(path + fileName); XWPFDocument document = new XWPFDocument(input); XHTMLOptions options = XHTMLOptions.create(); OutputStream out = new FileOutputStream(new File(path + fileName + ".html")); XHTMLConverter.getInstance().convert(document, out, options); }
/** * @param path 文件地址 * @param fileName 文件名 (word名) * @Description doc格式word转换 * @Throws * @Return void * @Date 2020-04-08 18:42:55 * @Author WangKun */ public static void docConvertHtml(String path, String fileName) throws Exception { InputStream input = new FileInputStream(new File(path + fileName)); HWPFDocument wordDocument = new HWPFDocument(input); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); wordToHtmlConverter.processDocument(wordDocument); Document htmlDocument = wordToHtmlConverter.getDocument(); File htmlFile = new File(path + "\\" + fileName + ".html"); OutputStream outStream = new FileOutputStream(htmlFile); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(outStream); TransformerFactory factory = TransformerFactory.newInstance(); Transformer serializer = factory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); outStream.close(); }