日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

[JavaWeb基础] 025.JAVA把word转换成html

發(fā)布時間:2025/4/14 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [JavaWeb基础] 025.JAVA把word转换成html 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

用第三方插件POIword文檔轉(zhuǎn)換成HTML,下面直接上代碼

package com.babybus.sdteam.wordtopdf;import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.List;import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.PicturesManager; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.hwpf.usermodel.PictureType; import org.jsoup.Jsoup;import org.w3c.dom.Document;public class WordToHtml {/*** 轉(zhuǎn)換word到html* * @param path* @return* @throws IOException* @throws FileNotFoundException* @throws ParserConfigurationException* @throws TransformerException*/public static String convertWordToHtml(String path)throws FileNotFoundException, IOException,ParserConfigurationException, TransformerException {// 轉(zhuǎn)換的結果路徑String htmlPath = "D://test//1.html";// 創(chuàng)建word文檔HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(path));// 兼容2007 以上版本// XSSFWorkbook xssfwork=new XSSFWorkbook(new FileInputStream(fileName));// 創(chuàng)建一個轉(zhuǎn)換器WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());// 設置圖片管理器wordToHtmlConverter.setPicturesManager(new PicturesManager() {public String savePicture(byte[] content, PictureType pictureType,String suggestedName, float widthInches, float heightInches) {return "test/" + suggestedName;}});// 處理word文檔wordToHtmlConverter.processDocument(wordDocument);// 保存圖片集合List pics = wordDocument.getPicturesTable().getAllPictures();if (pics != null) {for (int i = 0; i < pics.size(); i++) {Picture pic = (Picture) pics.get(i);try {pic.writeImageContent(new FileOutputStream("D:/test/"+ pic.suggestFullFileName()));} catch (FileNotFoundException e) {e.printStackTrace();}}}// 取出轉(zhuǎn)換的文檔Document htmlDocument = wordToHtmlConverter.getDocument();// 創(chuàng)建輸出流 和創(chuàng)建DOM源ByteArrayOutputStream out = new ByteArrayOutputStream();DOMSource domSource = new DOMSource(htmlDocument);StreamResult streamResult = new StreamResult(out);// 轉(zhuǎn)換工廠TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING, "GB2312");serializer.setOutputProperty(OutputKeys.INDENT, "yes");serializer.setOutputProperty(OutputKeys.METHOD, "HTML");serializer.transform(domSource, streamResult);out.close();// 寫入文件writeFile(new String(out.toByteArray()), htmlPath);return htmlPath;}/*** 寫入文件* * @param content* @param path*/public static void writeFile(String content, String path) {FileOutputStream fos = null;BufferedWriter bw = null;org.jsoup.nodes.Document doc = Jsoup.parse(content);content = doc.html();try {File file = new File(path);fos = new FileOutputStream(file);bw = new BufferedWriter(new OutputStreamWriter(fos, "GB2312"));bw.write(content);} catch (FileNotFoundException fnfe) {fnfe.printStackTrace();} catch (IOException ioe) {ioe.printStackTrace();} finally {try {if (bw != null)bw.close();if (fos != null)fos.close();} catch (IOException ie) {}}} }

?編碼必須用GB2312,用UTF8會有亂碼問題。

?

?

?

本站文章為?寶寶巴士 SD.Team?原創(chuàng),轉(zhuǎn)載務必在明顯處注明:(作者官方網(wǎng)站:?寶寶巴士?)?

轉(zhuǎn)載自【寶寶巴士SuperDo團隊】?原文鏈接:?http://www.cnblogs.com/superdo/p/4893022.html

轉(zhuǎn)載于:https://www.cnblogs.com/superdo/p/4893022.html

《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的[JavaWeb基础] 025.JAVA把word转换成html的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。