資源簡介
將word轉(zhuǎn)換成html代碼,包含doc和docx格式,以及所需jar包
代碼片段和文件信息
package?com.unibank.webad.utils;
import?java.io.ByteArrayOutputStream;
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
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.HWPFDocument;
import?org.apache.poi.hwpf.converter.PicturesManager;
import?org.apache.poi.hwpf.converter.WordToHtmlConverter;
import?org.apache.poi.hwpf.usermodel.PictureType;
??
import?org.apache.poi.xwpf.converter.core.BasicURIResolver;
import?org.apache.poi.xwpf.converter.core.FileImageExtractor;
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;
import?com.unibank.webad.Config;
public?class?WordToHtml?{
/**
?*?2003?doc文件轉(zhuǎn)換html
?*?@param?inputStream
?*?@param?filepath??當(dāng)word中有圖片的時(shí)候,將圖片存儲的路徑
?*?@return
?*/
public?static?String?convertDoc2Html(InputStream?inputStreamString?filepath){
if(inputStream!=null){
??
?final?File?catalog?=?new?File(Config.ADVERTFILE_PATH?+?“/“?+?filepath);
if?(catalog!=null?&&?!catalog.exists())?{
catalog.mkdirs();
}
ByteArrayOutputStream?outStream=new?ByteArrayOutputStream();
try?{
HWPFDocument?wordDocument?=?new?HWPFDocument(inputStream);
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)?{
File?file?=?new?File(catalog.getPath()+?“/“+?suggestedName);
try?{
OutputStream?os?=?new?FileOutputStream(file);
os.write(content);
os.close();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}
?????????????
//獲取http?地址
String?abPath=file.getAbsolutePath();
String?file_uri=abPath.substring(abPath.indexOf(“file“)+4?abPath.length());
file_uri=file_uri.replaceAll(“\\\\“?“/“);
String?path?=?Config.ADVERTFILE_READPATH?+?file_uri;
????return?path;
}
});
?????????
wordToHtmlConverter.processDocument(wordDocument);
Document?htmlDocument?=?wo
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-05-26?14:54??word轉(zhuǎn)換成html\
?????文件????14841622??2016-05-25?16:40??word轉(zhuǎn)換成html\ooxm
?????文件??????153217??2013-10-25?08:01??word轉(zhuǎn)換成html\org.apache.poi.xwpf.converter.core-1.0.1.jar
?????文件???????50397??2016-05-25?15:56??word轉(zhuǎn)換成html\org.apache.poi.xwpf.converter.xhtml-1.0.0.jar
?????文件?????1820323??2012-03-17?18:06??word轉(zhuǎn)換成html\poi-3.8-20120326.jar
?????文件???????30446??2012-03-17?18:06??word轉(zhuǎn)換成html\poi-excelant-3.8-20120326.jar
?????文件??????933010??2012-03-17?18:06??word轉(zhuǎn)換成html\poi-ooxm
?????文件?????4706775??2012-03-17?18:06??word轉(zhuǎn)換成html\poi-ooxm
?????文件?????1186887??2012-03-17?18:06??word轉(zhuǎn)換成html\poi-scratchpad-3.8-20120326.jar
?????文件????????8214??2016-05-26?14:52??word轉(zhuǎn)換成html\WordToHtml.java
評論
共有 條評論