java poi- 实现 word Excel pdf ppt 转 HTML
来源:互联网 发布:淘宝店铺封号 编辑:程序博客网 时间:2024/05/22 06:05
所需要 jarpoi-3.17.jarpoi-examples-3.17.jarpoi-excelant-3.17.jarpoi-ooxml-3.17.jarpoi-ooxml-schemas-3.15.0.jardom4j-1.6.1.jarpoi-scratchpad-3.17.jarxmlbeans-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);"+" onclick="+"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);"+" onclick="+"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——true command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf) // ttf,otf,woff,svg command.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——true command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf) // ttf,otf,woff,svg command.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);"+" onclick="+"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);"+" onclick="+"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;}
阅读全文
0 0
- java poi- 实现 word Excel pdf ppt 转 HTML
- java实现word,ppt,excel转pdf
- POI pdf ppt word excel
- java实现在线预览--poi实现word、excel、ppt转html
- java实现word,ppt,excel,jpg转pdf
- java实现word,ppt,excel,jpg转pdf
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- .NET WEB 实现office文档(word、excel、ppt)转PDF
- Word.Excel.ppt转html
- java(word/ppt/excel)转换成HTML实现
- c# word ppt excel转pdf
- c# word ppt excel转pdf
- 常见word,excel,ppt 转pdf
- PDF免费转Word、PPT、Excel
- java+poi实现word转html显示
- java poi 将word转成html,ppt转成图片
- asp.net实现word、excel、ppt、pdf在线预览
- .net 实现word、excel、ppt、pdf预览功能
- 字符串常用的方法(备忘)
- Pugixml一种快速解析XML文件的开源解析库《转载》
- bad ELF interpreter: No such file or directory
- 征信的3个谣言,我不说你肯定都信了!
- Linux备忘
- java poi- 实现 word Excel pdf ppt 转 HTML
- 753个大奖邀请各位新老博主入驻云栖,请技术人员广而告之
- 近日P2P平台扑街退出成潮,抵押类投资项目却优势凸显
- org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler error 严重: Exception occurred during proc
- 恶意代码分析实战 Lab 9-1 习题笔记
- 字符之间的转换
- SpringMVC+Spring+mybatis+redis项目从零开始--redis缓存策略和配置实现
- 博客开篇——JAVA虚拟机,双亲委派模型
- 阿里云企业邮箱实现发邮件验证