Java:POI方式实现Word转html/htm
2019獨角獸企業重金招聘Python工程師標準>>>
這里就不對POI做過多的說明了,貼個官網 https://poi.apache.org/,隨意看看。
首先搞清楚下要將doc/docx文檔轉成html/htm的話要怎么處理,根據POI的文檔,我們可以知道,處理doc 格式文件對應的 POI API 為 HWPF、docx 格式為 XWPF。此處參考下這篇好文:http://www.open-open.com/lib/view/open1389594797523.html?在格式轉換上說得很清楚。
所以整體就是:根據文檔類型,doc我們用HWPF對象處理轉換、docx用XWPF對象處理轉換。
順便貼一下這個在線文檔 http://poi.apache.org/apidocs/index.html,不得不說看得相當麻煩,特別是XWPF的。
一、處理doc。
這個相對簡單,網上一查一堆,我的代碼也是根據網上的做下自己的優化和邏輯。
因為POI很早前就可以支持doc的處理,所以資料比較多。
思路就是:HWPFDocument對象實例化文件流 ->?WordToHtmlConverter對象處理HWPFDocument對象及預處理頁面的圖片等(主要是圖片)
文檔說明是:
Converts Word files (95-2007) into HTML files. This implementation doesn't create images or links to them. This can be changed by overriding AbstractWordConverter.processImage(Element, boolean, Picture) method.->?org.w3c.dom.Document對象處理WordToHtmlConverter,生成DOM對象 -> 輸出文件。
這里有個好處就是使用到了Document對象,從而解決了編碼、文件格式等問題。
這里因為過程簡單,直接貼簡單demo,看注釋即可:
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List;import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;import org.apache.commons.io.FileUtils; 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.Picture; import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.core.FileURIResolver; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFPictureData; import org.w3c.dom.Document;public class POIForeViewUtil {public void parseDocx2Html() throws Throwable {final String path = "F:\\";final String file = "xxxxxxx.doc";InputStream input = new FileInputStream(path + file);String suffix = file.substring(file.indexOf(".")+1);// //截取文件格式名//實例化WordToHtmlConverter,為圖片等資源文件做準備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 suggestedName;}});if ("doc".equals(suffix.toLowerCase())) {// docxHWPFDocument wordDocument = new HWPFDocument(input);wordToHtmlConverter.processDocument(wordDocument);//處理圖片,會在同目錄下生成 image/media/ 路徑并保存圖片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(path+ pic.suggestFullFileName()));} catch (FileNotFoundException e) {e.printStackTrace();}}}} // 轉換Document htmlDocument = wordToHtmlConverter.getDocument();ByteArrayOutputStream outStream = new ByteArrayOutputStream();DOMSource domSource = new DOMSource(htmlDocument);StreamResult streamResult = new StreamResult(outStream);TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");//編碼格式serializer.setOutputProperty(OutputKeys.INDENT, "yes");//是否用空白分割serializer.setOutputProperty(OutputKeys.METHOD, "html");//輸出類型serializer.transform(domSource, streamResult);outStream.close();String content = new String(outStream.toByteArray());FileUtils.writeStringToFile(new File(path, "interface.html"), content,"utf-8");}public static void main(String[] args) throws Throwable {new POIForeViewUtil().parseDocx2Html();}}接著看第二種
二、處理docx。
docx是07的版本,處理起來困難的多,貌似POI對docx的處理方法沒有doc那么便捷,處理樣式等等都有問題,我遇到的兩個最明顯問題就是字體編碼問題和表格的邊框線顯示。
思路:XWPFDocument加載文件流 ->?XHTMLOptions處理頁面資源(主要圖片) ->?OutputStream輸出流直接輸出文件。
過程代碼相當簡單,可是越簡單結果約沒有預期的好。輸出的文件字體編碼默認為GBK,例如我的“微軟雅黑”字體就變成“寰蔣闆呴粦”,而且節點的顯示也沒有doc處理的好。
同樣貼一下demo代碼:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult;import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.core.FileURIResolver; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFPictureData;public class Word07ToHtml {public static void parseToHtml() throws IOException {File f = new File("F:/xxxxx.docx");if (!f.exists()) {System.out.println("Sorry File does not Exists!");} else {if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {// 1) 加載XWPFDocument及文件InputStream in = new FileInputStream(f);XWPFDocument document = new XWPFDocument(in);// 2) 實例化XHTML內容(這里將會把圖片等文件放到生成的"word/media"目錄)File imageFolderFile = new File("f:/opt");XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));options.setExtractor(new FileImageExtractor(imageFolderFile));//options.setIgnoreStylesIfUnused(false);//options.setFragment(true);// 3) 將XWPFDocument轉成XHTML并生成文件OutputStream out = new FileOutputStream(new File("F:/result.html"));XHTMLConverter.getInstance().convert(document, out, null);} else {System.out.println("Enter only MS Office 2007+ files");}}}public static void main(String args[]) {try {//String string = new String("寰蔣闆呴粦".getBytes("GBK"), "UTF-8");//System.out.println(string);parseToHtml();} catch (IOException e) {e.printStackTrace();}} }由于已將兩個Demo移出項目,沒有截圖。
POI的jar包下載路徑:
https://archive.apache.org/dist/poi/release/bin/poi-bin-3.9-20121203.zip
轉載于:https://my.oschina.net/u/2428684/blog/842930
總結
以上是生活随笔為你收集整理的Java:POI方式实现Word转html/htm的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Glide核心设计一:皮皮虾,我们走
- 下一篇: RTEMS移植USB无线网卡的设想