美解決doc、docx格式word轉換為Html
阿新 • • 發佈:2018-11-02
由於專案需要,需要線上預覽文件,所以就想轉換為htmL格式;
此專案為maven專案,引入的包可能需要一些時間;
maven專案轉換為eclipse專案命令是:mvn eclipse:eclipse,然後匯入到eclipse;
jar引入完成後,絕對100%能執行;
最後附上專案下載地址;
主要程式碼:
package fxma.Word2Html; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; 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.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.commons.io.output.ByteArrayOutputStream; 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.model.PicturesTable; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow; 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.w3c.dom.Document; /** * * @author Mafx * */ public class Word2Html { private final static String tempPath = "c:/test/"; public static void main(String argv[]) { try { doc2Html("c://bb.doc", "c://bb.html"); docx2Html("c://aa.docx", "c:aa.html"); } catch (Exception e) { e.printStackTrace(); } } /** * doc轉換為html * * @param fileName * @param outPutFile * @throws TransformerException * @throws IOException * @throws ParserConfigurationException */ public static void doc2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { long startTime = System.currentTimeMillis(); HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); 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; } }); wordToHtmlConverter.processDocument(wordDocument); // 儲存圖片 List<Picture> pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get(i); System.out.println(); try { pic.writeImageContent(new FileOutputStream(tempPath + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(out); 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); out.close(); writeFile(new String(out.toByteArray()), outPutFile); System.out.println("Generate " + outPutFile + " with " + (System.currentTimeMillis() - startTime) + " ms."); } /** * 寫檔案 * * @param content * @param path */ public static void writeFile(String content, String path) { FileOutputStream fos = null; BufferedWriter bw = null; try { File file = new File(path); fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8")); 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) { } } } /** * docx格式word轉換為html * * @param fileName * docx檔案路徑 * @param outPutFile * html輸出檔案路徑 * @throws TransformerException * @throws IOException * @throws ParserConfigurationException */ public static void docx2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { String fileOutName = outPutFile; long startTime = System.currentTimeMillis(); XWPFDocument document = new XWPFDocument(new FileInputStream(fileName)); XHTMLOptions options = XHTMLOptions.create().indent(4); // 匯出圖片 File imageFolder = new File(tempPath); options.setExtractor(new FileImageExtractor(imageFolder)); // URI resolver options.URIResolver(new FileURIResolver(imageFolder)); File outFile = new File(fileOutName); outFile.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(outFile); XHTMLConverter.getInstance().convert(document, out, options); System.out.println("Generate " + fileOutName + " with " + (System.currentTimeMillis() - startTime) + " ms."); } }
資源路徑:http://download.csdn.net/detail/renzhehongyi/9142883