import java.io.ByteArrayOutputStream;
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.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.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.w3c.dom.Document;
public class WordToHtml {
/**
* @param args
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
final String path = "d:/temp/";
final String file = "1.doc";
InputStream input = new FileInputStream(path + file);
HWPFDocument wordDocument = new HWPFDocument(input);
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(path + suggestedName);
try {
OutputStream os = new FileOutputStream(file);
os.write(content);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path + suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
File htmlFile = new File(path + "1.html");
OutputStream outStream = new FileOutputStream(htmlFile);
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();
}
}
详细解决方案
运用java框架POI将word转换成html格式
热度:698 发布时间:2013-04-12 18:33:11.0
相关解决方案
- java 乱码 汉字是 ? 如何转换成汉字
- java web 登录次数限制,该如何解决
- java 工商银行网银支付 B2B的 都亟需什么,需要注意什么
- Java Applet程序从JDK6升级到7时遇到的有关问题
- java web 视频相干
- java 系统单点登录解决方案
- java 获取客户端IP解决办法
- JAVA 后台怎么获得前台页面FCKedit编辑器中的内容以及内容的样式
- java 正则化匹配有关问题
- JAVA WEB导航条,该怎么处理
- java 中Node 有关问题
- 吐了,java Timer 终止不了。
- java web开发解决办法
- 关于 java cast 有关问题,你明白不
- java ftp 下传和上载乱码有关问题
- java web项目上的一些文件
- Hibernate操作数据库 报错java.lang.Integer cannot be cast to java.lang.String如何改
- java struts 文件上载
- java web baidu map开发
- JAVA WEB导航条解决办法
- java 编写ftp上载
- java web中的一个有关问题,困扰小弟我很久了
- java web 与tomcat解决办法
- java web基于j2ee的一些有关问题
- java 内网外网的一个需求,该怎么处理
- jsp 页面 安插ArrayList 报错。为什么,已经加了import="java.util."了呀!
- java dwg 怎样用java把dwg便是aoutcad的格式文件转换成gif,jpg等格式
- 求《Java Web 程序设计与项目实践》解决办法
- java webservice解决方法
- java 与jsp,该怎么解决