使⽤较新poi版本实现word转html
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
l.parsers.DocumentBuilderFactory;
l.transform.OutputKeys;
l.transform.Transformer;
l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.transform.stream.StreamResult;
import org.apachemons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.verter.WordToHtmlConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import fr.opensagres.verter.xhtml.XHTMLConverter;
import fr.opensagres.verter.xhtml.XHTMLOptions;
/**
* word⽂档转成html
*
* @author lijianghuai
*/
public class Word2HtmlUtil {
private static final Logger log = Logger(Name());
/**
* docx to html
*
* @param in 输⼊流
* @return
* @throws IOException
*/
public static InputStream docxToHtml(InputStream in){
XWPFDocument document = null;
try{
document =new XWPFDocument(in);
XHTMLOptions options = ate();
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
ByteArrayOutputStream out =new ByteArrayOutputStream();
return new ByteArray());
}catch(IOException e){
<(e.getMessage(), e);
}
return in;
}
/**
* doc to html
*
* @param in
* @returnhtml document是什么
* @throws Exception
*/
public static InputStream docToHtml(InputStream in){
try{
HWPFDocument wordDocument =new HWPFDocument(in);
WordToHtmlConverter wordToHtmlConverter =new WordToHtmlConverter(
wordToHtmlConverter.processDocument(wordDocument);
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = Document();
ByteArrayOutputStream outStream =new ByteArrayOutputStream();
DOMSource domSource =new DOMSource(htmlDocument);
StreamResult streamResult =new StreamResult(outStream);
TransformerFactory factory = wInstance();
Transformer serializer = wTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
serializer.setOutputProperty(OutputKeys.METHOD,"html");
outStream.close();
return new ByteArray());
}catch(Exception e){
<(e.getMessage(), e);
}
return in;
}
public static InputStream word2Html(Map<String, Object> request, InputStream in){
String fileName =(String) ("name");
if(StringUtils.isEmpty(fileName)){
return in;
}
String extensionName = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
String prevName = fileName.substring(0, fileName.lastIndexOf("."));
if("html".equalsIgnoreCase(extensionName)){
return in;
}
if(extensionName.equalsIgnoreCase("doc")){
request.put("name",prevName +".html");
return docToHtml(in);
}
if(extensionName.equalsIgnoreCase("docx")){
request.put("name",prevName +".html");
return docxToHtml(in);
}
return in;
}
}
需要的依赖:
/**
poi3.17及以上,3.17以下的转换帖⼦到处都是。操作都⼀样,只是注意依赖包
*/
compile “org.apache.poi:poi:3.17”
compile “org.apache.poi:poi-scratchpad:3.17”
compile “org.apache.poi:poi-ooxml:3.17”
compile “fr.opensagres.xdocreport:fr.opensagres.:2.0.1”
compile “fr.opensagres.xdocreport:fr.opensagres.verter.xhtml:2.0.1”

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。