poi word转html 在线阅读
2017-01-16 12:02
232 查看
下的是 poi-bin-3.14zip
把这5个jar包 都解压出来,加入到 工程中
最后还要用到这样一个jar文件FileUtils类
功能是实现了将一个word文档(doc)转成了html,后面就可以通过html进行在线观看了。不过不能对word进行操作。
代码如下:
把这5个jar包 都解压出来,加入到 工程中
最后还要用到这样一个jar文件FileUtils类
功能是实现了将一个word文档(doc)转成了html,后面就可以通过html进行在线观看了。不过不能对word进行操作。
代码如下:
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; 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.FileUtils; 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.w3c.dom.Document; public class PoiWordToHtml { public static void main(String[] args) throws Throwable { final String path = "D:\\桌面\\"; 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) { return suggestedName; } }); wordToHtmlConverter.processDocument(wordDocument); List pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get(i); try { pic.writeImageContent(new FileOutputStream(path + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 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(); String content = new String(outStream.toByteArray()); FileUtils.write(new File(path, "1.html"), content, "utf-8"); } }
相关文章推荐
- 详解Html a标签中href和onclick用法、区别、优先级别
- html田字格
- HTML动态年份下拉列表
- You can add this extension to convert your html code to a regular string:
- Java 过滤 html、script、style 代码得到纯字符串 方法
- html里name、id和class的区别
- html标签的嵌套规则分析
- html中点击a标签获取text
- document.write和 innerHTML的区别
- [技术] crm分页列表显示. {word笔记转html复制}
- HTML滚动条
- HTML DOM与核心DOM的区别
- SVG(可缩放矢量图形)虚线相关属性与线条动画原理:一条会动的线
- html中 如何获取屏幕参数??
- HTML基础知识(六)
- jmeter+ant+jenkins+mac使用HTML Publisher插件后查看html报告显示不正常
- HTML-框架
- html小知识
- html——超链接
- SVG(可缩放矢量图形)绘制工具Method Draw