Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Word Export html to realize online Preview

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The required maven dependencies are compiled Must match the version: fr.opensagres.xdocreport org.apache.poi.xwpf.converter.core 1.0.5 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.5 org.apache.commons commons-io 1.3.2 org.apache.poi Poi-scratchpad 3.17 org.apache.commons commons-collections4 4.0 org.apache.poi poi 3.17 org.apache.xmlbeans xmlbeans 2.6.0 org.apache.poi poi-ooxml 3.14 org .apache.commons commons-lang3 3.4 package com.zyhao.openec.excel.utils

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.InputStream

Import java.io.OutputStreamWriter

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.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.usermodel.Picture

Import org.apache.poi.hwpf.usermodel.PictureType

Import org.apache.poi.xwpf.converter.core.FileImageExtractor

Import org.apache.poi.xwpf.converter.core.IURIResolver

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

Public class WordToHtml {

Public static void main (String [] args) {

Try {

WordToHtml ("E:\ me\ 2.docx", "E:\ me\", "123.html")

/ / wordToHtml ("E:\ me\ 2.doc", "E:\ me\", "12.html")

} catch (Exception e) {

E.printStackTrace ()

}

}

Public static void wordToHtml (String wordPath,String htmlPath,String newFilename) throws Exception {convert2Html (wordPath, htmlPath, newFilename);} public static void writeFile (String content, String path) throws Exception {FileOutputStream fos = null; BufferedWriter bw = null; try {File file = newFile (path); fos = new FileOutputStream (file); bw = new BufferedWriter (new OutputStreamWriter (fos)); 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) {} / * convert word to html * support .doc and .docx * @ param fileName word file name * @ param outPutFilePath html storage path * @ param newFileName html name * @ throws Exception * / public static void convert2Html (String fileName, String outPutFilePath String newFileName) throws Exception {String substring = fileName.substring (fileName.lastIndexOf (".") + 1) ByteArrayOutputStream out = new ByteArrayOutputStream (); / * * word2007 and word2003 are built differently. * the former is built by xml, while the latter is built by dom tree. * the suffix of the file is also different, the former suffix is .docx, the latter suffix is .doc * accordingly, apache.poi provides different implementation classes. * / if ("docx" .equals (substring)) {

/ / writeFile (new String ("Sorry, word document in .docx format, cannot generate preview" .getBytes ("utf-8"), outPutFilePath+newFileName)

/ / step 1: load DOCX into XWPFDocument InputStream inputStream = new FileInputStream (new File (fileName)); XWPFDocument document = new XWPFDocument (inputStream); / / step 2: prepare XHTML options final String imageUrl = ""; XHTMLOptions options = XHTMLOptions.create (); options.setExtractor (new FileImageExtractor (new File (outPutFilePath + imageUrl)); options.setIgnoreStylesIfUnused (false); options.setFragment (true); options.URIResolver (new IURIResolver () {

/ / @ Override rewrite method, plus this error report, you can see what the problem is

Public String resolve (String uri) {

Return imageUrl + uri

}

});

/ / step 3: convert XWPFDocument to XHTML XHTMLConverter.getInstance (). Convert (document, out, options);} else {HWPFDocument wordDocument = new HWPFDocument (new FileInputStream (fileName)); / / WordToHtmlUtils.loadDoc (new FileInputStream (inputFile)); 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;}}); wordToHtmlConverter.processDocument (wordDocument) / / save pictures List pics=wordDocument.getPicturesTable () .getAllPictures (); if (picsphotos filled with nullable boxes) pics.isEmpty () {for (int iSyst0polii)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report