java poi- 实现 word Excel pdf ppt 转 HTML

所需要 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——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 block
				e.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 dir
		Dimension 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 area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				// render
				ppt.getSlides().get(i).draw(graphics);
				// save the output
				String imageDir = targetDir + pptFileName + "\\";
				com.util.FileUtils.createDir(imageDir);// create image dir
				String 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 dir
			Dimension 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 area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				// render
				ppt.getSlides().get(i).draw(graphics);
				String imageDir = targetDir + pptFileName + "\\";
				com.util.FileUtils.createDir(imageDir);// create image dir
				String 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;
	}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值