当前位置 主页 > 网站技术 > 代码类 >

    java实现在线预览--poi实现word、excel、ppt转html的方法

    栏目:代码类 时间:2019-09-16 16:08

    java实现在线预览

    - -之poi实现word、excel、ppt转html,具体内容如下所示:

    ###简介

    java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office、office web 365(http://www.officeweb365.com/)他们都有云在线预览服务,就是要钱0.0

    如果想要免费的,可以用openoffice,还需要借助其他的工具(例如swfTools、FlexPaper等)才行,可参考这篇文章http://blog.csdn.net/z69183787/article/details/17468039,写的挺细的,实现原理就是:

    1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件;

    2.通过swfTools将pdf文件转换成swf格式的文件;

    3.通过FlexPaper文档组件在页面上进行展示。

    当然如果装了Adobe Reader XI,那把pdf直接拖到浏览器页面就可以直接打开预览,这样就不需要步骤2、3了,前提就是客户装了Adobe Reader XI这个pdf阅读器。

    我这里介绍通过poi实现word、excel、ppt转html,这样就可以放在页面上了。

    ###word转html

    package wordToHtml;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:\\poi-test\\wordToHtml\\"; final String file = "人员选择系分.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.writeStringToFile(new File(path, "人员选择系分.html"), content, "utf-8"); }}