1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java poi- 实现 word Excel pdf ppt 转 HTML

java poi- 实现 word Excel pdf ppt 转 HTML

时间:2022-01-28 23:55:34

相关推荐

java   poi- 实现 word Excel pdf  ppt  转 HTML

所需要 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);"+" ο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 = toImage(sourcePath, targetDir,id,webpath);com.util.FileUtils.writeFile(htmlStr, targetDir + "\\"+id+".html");} else if ("pptx".equals(type)) {String htmlStr = toImage(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 toImage(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 toImage(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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。