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

时间:2021-05-19

java实现在线预览

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

###简介

java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office、office web 365(http://mons.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的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章