所需要 jar
poi-3.17.jar
poi-examples-3.17.jar
poi-excelant-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.15.0.jar
dom4j-1.6.1.jar
poi-scratchpad-3.17.jar
xmlbeans-2.6.0.jar方法 传参:
/**
* filetype 文件类型
*
*ctxPath 路径 filelocalname 所要生成HTML的名称
* zid  是me 自定义的一个id
*/if("DOC".equals(filetype)){
FileUtil.folderfilehtml(ctxPath + filelocalname, zid, ctxPath);
}else if("XLS".equals(filetype)||"XLSX".equals(filetype)){
FileUtil.folderfileExceltohtml(ctxPath + filelocalname, zid, ctxPath);
}else if("PDF".equals(filetype)){
FileUtil.pdf2html("D:\\application\\pdf2htmlEX-v1.0\\pdf2htmlEX.exe",ctxPath + filelocalname,application.getRealPath("/")+DOWNLOADPATH.get("folderpdf"),zid+".html");
}else if("PPT".equals(filetype)){
FileUtil.pptToHtml(ctxPath + filelocalname, zid, ctxPath,webpath);
}//所调用的方法
/*** word 转换 HTML*
*/public static void folderfilehtml(String filename,String htmlid,String ctxPath) throws IOException, ParserConfigurationException, TransformerException{InputStream input = new FileInputStream(filename);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(ctxPath+ 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, "GB2312");serializer.setOutputProperty(OutputKeys.INDENT, "yes");serializer.setOutputProperty(OutputKeys.METHOD, "html");serializer.transform(domSource, streamResult);outStream.close();String content = new String(outStream.toByteArray());String conut =content.substring(0,content.lastIndexOf("</head>"));String countend=content.substring(content.indexOf("</head>")-1,content.length()); conut+="<input type="+"button"+" value="+"打印"+" href="+"javascript:void(0);"+" οnclick="+"window.print();"+"  class="+"noprint"+" style="+"height:30px;width: 60px; padding-right:5px;align:right;float:left;FONT-WEIGHT: bold;FONT-SIZE: 12pt;COLOR: #000000;FONT-FAMILY: Arial"+">";content=conut+countend;FileUtils.writeStringToFile(new File(ctxPath, htmlid+".html"), content);}/*** Excel 转换 HTML* */public static void folderfileExceltohtml(String filename,String htmlid,String ctxPath) throws IOException, ParserConfigurationException, TransformerException{InputStream input=new FileInputStream(filename);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 (ctxPath + 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, "GB2312");serializer.setOutputProperty (OutputKeys.INDENT, "yes");serializer.setOutputProperty (OutputKeys.METHOD, "html");serializer.transform (domSource, streamResult);outStream.close();String content = new String (outStream.toByteArray() );String conut =content.substring(0,content.lastIndexOf("</head>"));String countend=content.substring(content.indexOf("</head>")-1,content.length()); conut+="<input type="+"button"+" value="+"打印"+" href="+"javascript:void(0);"+" οnclick="+"window.print();"+"  class="+"noprint"+" style="+"height:30px;width: 60px; padding-right:5px;align:right;float:left;FONT-WEIGHT: bold;FONT-SIZE: 12pt;COLOR: #000000;FONT-FAMILY: Arial"+">";content=conut+countend;FileUtils.writeStringToFile(new File (ctxPath, htmlid+".html"), content);}/*** PDF 转 HTML*//** * 把输入流里面的内容以UTF-8编码当文本取出。 * 不考虑异常,直接抛出 * @param ises * @return * @throws IOException */  /*** 调用pdf2htmlEX将pdf文件转换为html文件* * @param exeFilePath*            pdf2htmlEX.exe文件路径* @param pdfFile*            pdf文件绝对路径* @param [destDir] 生成的html文件存放路径* @param htmlName*            生成的html文件名称* @return*/public static boolean pdf2html(String exeFilePath, String pdfFile,String destDir, String htmlFileName) {if (!(exeFilePath != null && !"".equals(exeFilePath) && pdfFile != null&& !"".equals(pdfFile) && htmlFileName != null && !"".equals(htmlFileName))) {System.out.println("传递的参数有误!");return false;}Runtime rt = Runtime.getRuntime();StringBuilder command = new StringBuilder();command.append(exeFilePath).append(" ");if (destDir != null && !"".equals(destDir.trim()))// 生成文件存放位置,需要替换文件路径中的空格command.append("--dest-dir ").append(destDir.replace(" ", "\" \"")).append(" ");command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0)command.append("--zoom 1.4 ");command.append("--process-outline 0 ");// html中显示链接:0——false,1——truecommand.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf)// ttf,otf,woff,svgcommand.append(pdfFile.replace(" ", "\" \"")).append(" ");// 需要替换文件路径中的空格if (htmlFileName != null && !"".equals(htmlFileName.trim())) {command.append(htmlFileName);if (htmlFileName.indexOf(".html") == -1)command.append(".html");}try {Process p = rt.exec(command.toString());StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");// 开启屏幕标准错误流errorGobbler.start();StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),"STDOUT");// 开启屏幕标准输出流outGobbler.start();int w = p.waitFor();int v = p.exitValue();if (w == 0 && v == 0) {return true;}} catch (Exception e) {e.printStackTrace();}return false;}public static boolean pdf2html_linux(String pdfFile, String destDir,String htmlFileName) {if (!(pdfFile != null && !"".equals(pdfFile) && htmlFileName != null && !"".equals(htmlFileName))) {System.out.println("传递的参数有误!");return false;}Runtime rt = Runtime.getRuntime();StringBuilder command = new StringBuilder();command.append("pdf2htmlEX").append(" ");if (destDir != null && !"".equals(destDir.trim()))// 生成文件存放位置,需要替换文件路径中的空格command.append("--dest-dir ").append(destDir.replace(" ", "\" \"")).append(" ");command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0)command.append("--process-outline 0 ");// html中显示链接:0——false,1——truecommand.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf)// ttf,otf,woff,svgcommand.append(pdfFile.replace(" ", "\" \"")).append(" ");// 需要替换文件路径中的空格if (htmlFileName != null && !"".equals(htmlFileName.trim())) {command.append(htmlFileName);if (htmlFileName.indexOf(".html") == -1)command.append(".html");}try {Process p = rt.exec(command.toString());StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");// 开启屏幕标准错误流errorGobbler.start();StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),"STDOUT");// 开启屏幕标准输出流outGobbler.start();int w = p.waitFor();int v = p.exitValue();if (w == 0 && v == 0) {return true;}} catch (Exception e) {e.printStackTrace();}return false;}/*** PPT 转 HTML* @param path* @param filename* @return*/public static void pptToHtml(String sourcePath,String id, String targetDir,String webpath) {File pptFile = new File(sourcePath);if (pptFile.exists()) {try {String type = com.util.FileUtils.GetFileExt(sourcePath);if ("ppt".equals(type)) {String htmlStr = toImage2003(sourcePath, targetDir,id,webpath);com.util.FileUtils.writeFile(htmlStr, targetDir + "\\"+id+".html");} else if ("pptx".equals(type)) {String htmlStr = toImage2007(sourcePath, targetDir, id,webpath);com.util.FileUtils.writeFile(htmlStr, targetDir + "\\"+id+".html");} else {System.out.println("the file is not a ppt");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {System.out.println("file does not exist!");}}public static String toImage2007(String sourcePath, String targetDir, String pptFileName,String webpath) throws Exception {String htmlStr = "";FileInputStream is = new FileInputStream(sourcePath);XMLSlideShow ppt = new XMLSlideShow(is);is.close();com.util.FileUtils.createDir(targetDir);// create html dirDimension pgsize = ppt.getPageSize();System.out.println(pgsize.width + "--" + pgsize.height);StringBuffer sb = new StringBuffer();for (int i = 0; i < ppt.getSlides().size(); i++) {try {// 防止中文乱码for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {if (shape instanceof XSLFTextShape) {XSLFTextShape tsh = (XSLFTextShape) shape;for (XSLFTextParagraph p : tsh) {for (XSLFTextRun r : p) {r.setFontFamily("宋体");}}}}BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = img.createGraphics();// clear the drawing areagraphics.setPaint(Color.white);graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));// renderppt.getSlides().get(i).draw(graphics);// save the outputString imageDir = targetDir + pptFileName + "\\";com.util.FileUtils.createDir(imageDir);// create image dirString imagePath = imageDir + pptFileName + "-" + (i + 1) + ".png";String imagewebpath = webpath+pptFileName+"\\"+ pptFileName + "-" + (i + 1) + ".png";sb.append("<html>");sb.append("<body>");sb.append("<input type="+"button"+" value="+"打印"+" href="+"javascript:void(0);"+" οnclick="+"window.print();"+"  class="+"noprint"+" style="+"height:30px;width: 60px; padding-right:5px;align:right;float:left;FONT-WEIGHT: bold;FONT-SIZE: 12pt;COLOR: #000000;FONT-FAMILY: Arial"+">");sb.append("<br />");sb.append("<p style=text-align:center;>");sb.append("<img src=" + "\"" + imagewebpath + "\"" + "/>");sb.append("</p>");sb.append("<br />");sb.append("</html>");sb.append("</body>");FileOutputStream out = new FileOutputStream(imagePath);javax.imageio.ImageIO.write(img, "png", out);out.close();} catch (Exception e) {System.out.println("第" + i + "张ppt转换出错");}}System.out.println("success");htmlStr = sb.toString();return htmlStr;}public static String toImage2003(String sourcePath, String targetDir, String pptFileName,String webpath) {String htmlStr = "";try {HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(sourcePath));com.util.FileUtils.createDir(targetDir);// create html dirDimension pgsize = ppt.getPageSize();StringBuffer sb = new StringBuffer();for (int i = 0; i < ppt.getSlides().size(); i++) {// 防止中文乱码for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {if (shape instanceof HSLFTextShape) {HSLFTextShape tsh = (HSLFTextShape) shape;for (HSLFTextParagraph p : tsh) {for (HSLFTextRun r : p) {r.setFontFamily("宋体");}}}}BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = img.createGraphics();// clear the drawing areagraphics.setPaint(Color.white);graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));// renderppt.getSlides().get(i).draw(graphics);String imageDir = targetDir + pptFileName + "\\";com.util.FileUtils.createDir(imageDir);// create image dirString imagePath = imageDir + pptFileName + "-" + (i + 1) + ".png";String imagewebpath = webpath +pptFileName+"\\"+ pptFileName + "-" + (i + 1) + ".png";sb.append("<html>");sb.append("<body>");sb.append("<input type="+"button"+" value="+"打印"+" href="+"javascript:void(0);"+" οnclick="+"window.print();"+"  class="+"noprint"+" style="+"height:30px;width: 60px; padding-right:5px;align:right;float:left;FONT-WEIGHT: bold;FONT-SIZE: 12pt;COLOR: #000000;FONT-FAMILY: Arial"+">");sb.append("<br />");sb.append("<p style=text-align:center;>");sb.append("<img src=" + "\"" + imagewebpath + "\"" + "/>");sb.append("</p>");sb.append("<br />");sb.append("</html>");sb.append("</body>");FileOutputStream out = new FileOutputStream(imagePath);javax.imageio.ImageIO.write(img, "png", out);out.close();}System.out.println("success");htmlStr = sb.toString();} catch (Exception e) {}return htmlStr;}

java poi- 实现 word Excel pdf ppt 转 HTML相关推荐

  1. office 文档 在线预览功能实现(word,excel,pdf,ppt等多种格式)——使用https://view.xdocin.com/view 提示文档过期——基础积累

    web实现office文档在线预览功能--基础积累 最近遇到一个需求,就是要实现多种文档链接的在线预览,最简单的方式就是通过window.open(url地址)的方式来实现. 但是如果要求是在一个弹窗 ...

  2. 微信公众号如何设置关键词回复Word/Excel/pdf/ppt等文件?

    大家好,我是运营小薇老师,今天给大家带来的课程是:<公众号如何设置关键词回复指定文件>,各位同学要认真听讲哦! 各位同学,大家都知道微信公众号有一个自动回复的功能,看看公众号后台,它可以设 ...

  3. Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)

    Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...

  4. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  5. txt doc rtf html,JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例.docx

    JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例??2012-06-2 ...

  6. java全文检索word中的内容_搜索引擎时对WORD,EXCEL,PDF,POWERPOINT文件全文检索的总结...

    搜索引擎时对WORD,EXCEL,PDF,POWERPOINT文件全文检索的总结 (2012-02-12 16:31:59) 标签: pdf文档 幻灯片 pdf文件 全文检索 控件 it JAVA读取 ...

  7. java操作word/excel/pdf等文件技术方案

    最近项目中遇到很多对word/excel/pdf等文件的操作,解决方案有好多,开源免费有:利用openoffice组件(需要安装openoffice软件),poi,itext等.也有收费的服务:asp ...

  8. JAVA POI处理WORD

    JAVA POI处理WORD 转载:http://w26.iteye.com/blog/974442 JavaApacheSVN 关键字:POI WORD HWPF WORD文本组成:文本.图.表和宏 ...

  9. 前端在线预览word,excel,pdf

    前端在线预览word,excel,pdf 预览Word 预览pdf 预览Excel 预览Word 微软的在线预览功能,可以预览word.ppt.Excel.PDF 局限: 需要外网能访问文件,如果是只 ...

最新文章

  1. 案例分析 | SAP如何帮助企业实现端到端的数字化供应链管理
  2. tableau暂时不支持m1芯片!期待未来!
  3. apache+Tomcat均衡负载配置
  4. angular组件--tips提示功能
  5. KUKA 声明变量时的几点注意
  6. freecplus框架-xml解析
  7. “狼牙抓鸡”现身IT江湖
  8. ios 开发证书导出p12文件_IOS开发 把cer证书转为p12证书
  9. iOS应用程序审核机制
  10. 支持8086c语言编程的编译器,8086汇编语言编程软件|8086汇编语言编译器(MKStudio) v1.0免费版 附安装教程_星星软件园...
  11. ens32文件空白(没有ens33文件)
  12. AirPlay屏幕镜像技术-概述-P1
  13. Linux 系统升级蝉道
  14. iOS 福利局之开发加速库分享
  15. pyqt5标签中的字设置不同字体_PyQt5 控件字体样式等设置的实现
  16. elementUI el-table 动态添加一行且保证每行数据相互独立,防止v-for影响每行
  17. Coursera | Andrew Ng (01-week-3-3.8)—激活函数的导数
  18. 复制粘贴技巧,全选快捷键ctrl加什么?
  19. html的 作用 是什么,html的作用
  20. 微信小程序 视频列表滑动无限循环(仿抖音)

热门文章

  1. android dex文件过多,意外的最高级别异常:com.android.dex.DexException:定义了多个dex文件...
  2. PageUtils后端常用分页工具类
  3. Deep3D: Fully Automatic 2D-to-3D Video Conversion with Deep Convolutional Neural Networks
  4. 常识整理-windows7旗舰版x86用激活程序激活失败,提示:请用管理员运行
  5. web页面 新消息提示音
  6. 2018 android最新版本,2008年至2018年,Android系统10年进化史
  7. 计算机毕业设计php公文审批系统-办公系统
  8. 博云信创云管平台入选工信部推荐解决方案名单,头部券商信创案例获应用示范单项
  9. python输入生日输出生肖_用Python输入年月日 输出X年X月X日生肖是XX,xx岁的XX星座...
  10. 19 Python __dict__与dir()区别