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

作者:yjclsx 时间:2022-09-29 20:29:41 

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");
}
}

###excel转html


package excelToHtml;
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.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.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 PoiExcelToHtml {
final static String path = "D:\\poi-test\\excelToHtml\\";
final static String file = "exportExcel.xls";
public static void main(String args[]) throws Exception {

InputStream input=new FileInputStream(path+file);
HSSFWorkbook excelBook=new HSSFWorkbook(input);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter (DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() );
excelToHtmlConverter.processWorkbook(excelBook);
List pics = excelBook.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 =excelToHtmlConverter.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, "exportExcel.html"), content, "utf-8");
}
}

###ppt转html

其实只是ppt转图片,有了图片后放到页面上去,点击下一页就一张张显示就可以了。这里只介绍ppt转图片的过程。


package pptToImg;
import java.awt.Dimension;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.awt.Color;  
import java.awt.Dimension;  
import java.awt.Graphics2D;  
import java.awt.geom.Rectangle2D;  
import java.awt.image.BufferedImage;  
import org.apache.poi.hslf.model.TextRun;  
import org.apache.poi.hslf.record.Slide;  
import org.apache.poi.hslf.usermodel.RichTextRun;  
import org.apache.poi.hslf.usermodel.SlideShow;  
public class PPTtoImage {  
 public static void main(String[] args) {  
   // 读入PPT文件  
   File file = new File("D:/poi-test/pptToImg/test.ppt");  
   doPPTtoImage(file);  
 }  
 public static boolean doPPTtoImage(File file) {  
   boolean isppt = checkFile(file);  
   if (!isppt) {  
     System.out.println("The image you specify don't exit!");  
     return false;  
   }  
   try {  
     FileInputStream is = new FileInputStream(file);  
     SlideShow ppt = new SlideShow(is);  
     is.close();  
     Dimension pgsize = ppt.getPageSize();  
     org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();  
     for (int i = 0; i < slide.length; i++) {  
       System.out.print("第" + i + "页。");  
       TextRun[] truns = slide[i].getTextRuns();  
       for ( int k=0;k<truns.length;k++){  
         RichTextRun[] rtruns = truns[k].getRichTextRuns();  
        for(int l=0;l<rtruns.length;l++){  
           int index = rtruns[l].getFontIndex();  
           String name = rtruns[l].getFontName();        
           rtruns[l].setFontIndex(1);  
           rtruns[l].setFontName("宋体");
//            System.out.println(rtruns[l].getText());
         }  
       }  
       BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);  
       Graphics2D graphics = img.createGraphics();  
       graphics.setPaint(Color.BLUE);  
       graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));  
       slide[i].draw(graphics);  
       // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径  
       FileOutputStream out = new FileOutputStream("D:/poi-test/pptToImg/pict_"+ (i + 1) + ".jpeg");  
       javax.imageio.ImageIO.write(img, "jpeg", out);  
       out.close();  
     }  
     System.out.println("success!!");  
     return true;  
   } catch (FileNotFoundException e) {  
     System.out.println(e);  
     // System.out.println("Can't find the image!");  
   } catch (IOException e) {  
   }  
   return false;  
 }  
 // function 检查文件是否为PPT  
 public static boolean checkFile(File file) {  
   boolean isppt = false;  
   String filename = file.getName();  
   String suffixname = null;  
   if (filename != null && filename.indexOf(".") != -1) {  
     suffixname = filename.substring(filename.indexOf("."));  
     if (suffixname.equals(".ppt")) {  
       isppt = true;  
     }  
     return isppt;  
   } else {  
     return isppt;  
   }  
 }  
}

ppt转图片有个缺陷,就是ppt里不是宋体的字有些可能会变成框框。

以上都需要引入poi的jar包。

要实现在线预览,只需把转换得到的html在新标签页打开或者镶嵌到某块区域就可以展现了。

总结

以上所述是小编给大家介绍的java实现在线预览--poi实现word、excel、ppt转html的方法,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://blog.csdn.net/yjclsx/article/details/51441632

标签:java,在线,预览,poi,html
0
投稿

猜你喜欢

  • Android OkHttp实现全局过期token自动刷新示例

    2023-01-24 10:31:57
  • Handler实现倒计时功能

    2021-06-01 15:24:31
  • Swagger2配置方式(解决404报错)

    2022-08-30 17:21:13
  • Spring Cloud负载均衡及远程调用实现详解

    2021-10-16 01:11:27
  • idea设置JVM运行参数的几种方式

    2023-05-26 14:57:33
  • Android编程之防止反编译的实现方法

    2021-10-31 04:23:35
  • java实现计算器功能

    2022-10-28 06:52:09
  • Android权限询问的实例详解

    2022-10-03 21:28:51
  • Logback动态修改日志级别的方法

    2023-05-30 21:47:01
  •  Java图形化界面编程实现简单计算器

    2021-06-06 17:23:09
  • JavaMail入门教程之接收邮件(4)

    2023-11-09 03:12:34
  • Android App支付系列(二):支付宝SDK接入详细指南(附官方支付demo)

    2022-03-14 01:12:28
  • Java监听器ActionListener与MouseListener的执行顺序说明

    2022-02-04 20:08:23
  • c/c++ 标准库 bind 函数详解

    2023-03-29 09:10:39
  • 如何在IDEA中对 hashCode()和 equals() 利用快捷键快速进行方法重写

    2021-11-04 19:06:13
  • Mybatis初始化知识小结

    2023-11-01 13:59:27
  • java判断回文数示例分享

    2023-03-20 03:18:22
  • 解决myBatis返回integer值的问题

    2022-07-23 18:17:38
  • Android之ArcSlidingHelper制作圆弧滑动效果

    2021-07-23 03:10:24
  • System.getProperty(user.dir)定位问题解析

    2023-11-20 08:50:44
  • asp之家 软件编程 m.aspxhome.com