¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.product.file.util; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.math.RoundingMode; |
| | | import java.text.DecimalFormat; |
| | | |
| | | import com.aspose.cells.PdfSaveOptions; |
| | | import com.aspose.cells.Workbook; |
| | | import com.aspose.slides.Presentation; |
| | | import com.aspose.words.Document; |
| | | import com.product.core.exception.BaseException; |
| | | import com.product.core.spring.context.SpringMVCContextHolder; |
| | | |
| | | public class AsposeUtil { |
| | | //æ¯å¦åå§äºlicense |
| | | private static boolean isInitLicense=false; |
| | | /** |
| | | * è·ålicense |
| | | * @return |
| | | */ |
| | | public static boolean getLicense(int type)throws BaseException { |
| | | //å·²ç»åå§è¿ï¼ä¸ååå§ |
| | | if(isInitLicense) { |
| | | return true; |
| | | } |
| | | boolean result = true; |
| | | InputStream is = null; |
| | | try { |
| | | is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); |
| | | com.aspose.cells.License excel = new com.aspose.cells.License(); |
| | | excel.setLicense(is); |
| | | |
| | | is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); |
| | | com.aspose.words.License word = new com.aspose.words.License(); |
| | | word.setLicense(is); |
| | | |
| | | is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); |
| | | com.aspose.slides.License ppt = new com.aspose.slides.License(); |
| | | ppt.setLicense(is); |
| | | |
| | | isInitLicense=true; |
| | | } catch (Exception e) { |
| | | result=false; |
| | | throw new BaseException(e); |
| | | }finally{ |
| | | if(is!=null) { |
| | | try { |
| | | is.close(); |
| | | }catch(IOException e) { |
| | | throw new BaseException(e); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | /** |
| | | * |
| | | * @param officePath |
| | | * @param OutPutPath |
| | | * @throws BaseException |
| | | */ |
| | | public static void Excel2Pdf(String officePath,String OutPutPath)throws BaseException { |
| | | // éªè¯License |
| | | if (!getLicense(1)) { |
| | | return; |
| | | } |
| | | FileOutputStream fileOS=null; |
| | | try { |
| | | File file = new File(OutPutPath); |
| | | if (!file.exists()) { |
| | | file.mkdirs(); |
| | | } |
| | | Workbook wb = new Workbook(officePath);// åå§excelè·¯å¾ |
| | | File pdfFile = new File(OutPutPath);// è¾åºè·¯å¾ |
| | | fileOS = new FileOutputStream(pdfFile); |
| | | //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF); |
| | | PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); |
| | | pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); |
| | | wb.save(fileOS, pdfSaveOptions); |
| | | } catch (Exception e) { |
| | | throw new BaseException(e); |
| | | }finally{ |
| | | if(fileOS!=null) { |
| | | try { |
| | | fileOS.flush(); |
| | | fileOS.close(); |
| | | }catch(IOException e) { |
| | | throw new BaseException(e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * |
| | | * @param officePath |
| | | * @param OutPutPath |
| | | * @throws BaseException |
| | | */ |
| | | public static void Word2Pdf(String officePath,String OutPutPath) throws BaseException { |
| | | // éªè¯License |
| | | if (!getLicense(2)) { |
| | | return; |
| | | } |
| | | FileOutputStream fileOS=null; |
| | | try { |
| | | File file = new File(OutPutPath); |
| | | if (!file.getParentFile().exists()) { |
| | | file.getParentFile().mkdirs(); |
| | | } |
| | | Document doc = new Document(officePath);// åå§wordè·¯å¾ |
| | | File pdfFile = new File(OutPutPath);// è¾åºè·¯å¾ |
| | | fileOS = new FileOutputStream(pdfFile); |
| | | doc.save(fileOS, com.aspose.words.SaveFormat.PDF); |
| | | } catch (Exception e) { |
| | | throw new BaseException(e); |
| | | }finally{ |
| | | if(fileOS!=null) { |
| | | try { |
| | | fileOS.flush(); |
| | | fileOS.close(); |
| | | }catch(IOException e) { |
| | | throw new BaseException(e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * |
| | | * @param officePath |
| | | * @param OutPutPath |
| | | * @throws BaseException |
| | | */ |
| | | public static void PPT2Pdf(String officePath,String OutPutPath)throws BaseException { |
| | | // éªè¯License |
| | | if (!getLicense(3)) { |
| | | return; |
| | | } |
| | | FileOutputStream fileOS=null; |
| | | try { |
| | | File PathFile = new File(OutPutPath); |
| | | if (!PathFile.exists()) { |
| | | PathFile.mkdirs(); |
| | | } |
| | | InputStream slides = new FileInputStream(new File(officePath));// åå§pptè·¯å¾ |
| | | Presentation pres = new Presentation(slides); |
| | | File file = new File(OutPutPath);// è¾åºpdfè·¯å¾ |
| | | fileOS = new FileOutputStream(file); |
| | | pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf); |
| | | fileOS.flush(); |
| | | fileOS.close(); |
| | | } catch (Exception e) { |
| | | throw new BaseException(e); |
| | | }finally{ |
| | | if(fileOS!=null) { |
| | | try { |
| | | fileOS.flush(); |
| | | fileOS.close(); |
| | | }catch(IOException e) { |
| | | throw new BaseException(e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * ææ¡£è½¬æ¢ |
| | | * @param officePath |
| | | * @param outFile |
| | | * @return |
| | | * @throws BaseException |
| | | */ |
| | | public static String OfficeToPdf(String officePath,String outFile,String officeType )throws BaseException { |
| | | |
| | | SpringMVCContextHolder.getSystemLogger().info("The file of office type:"+officePath); |
| | | SpringMVCContextHolder.getSystemLogger().info("The file of PDF type:"+outFile); |
| | | File file = new File(officePath); |
| | | //夿å½åofficeæä»¶æ¯å¦å·²ç»è½¬ä¸ºPDF,å¦æå·²è½¬ä¸ºPDFå°±ä¸éè¦å次转æ¢ã |
| | | if (file.exists()&& !"pdf".equals(officeType)) { |
| | | double bytes = file.length(); |
| | | double kilobytes = (bytes / 1024); |
| | | double megabytes = (kilobytes / 1024); |
| | | |
| | | DecimalFormat df = new DecimalFormat("0.00"); |
| | | df.setRoundingMode(RoundingMode.HALF_UP); |
| | | String MB = df.format(megabytes); |
| | | Double Size = Double.parseDouble(MB); |
| | | // if(Size>10){ |
| | | SpringMVCContextHolder.getSystemLogger().info("The file size:"+Size+"MB"); |
| | | // } |
| | | //"doc", "docx", "xls","xlsx", "ppt", "pptx" |
| | | if(officeType.equals("doc")||officeType.equals("docx")){ |
| | | Word2Pdf(officePath,outFile); |
| | | }else if(officeType.equals("xls")||officeType.equals("xlsx")){ |
| | | Excel2Pdf(officePath,outFile); |
| | | }else if(officeType.equals("ppt")||officeType.equals("pptx")){ |
| | | PPT2Pdf(officePath,outFile); |
| | | }else{ |
| | | SpringMVCContextHolder.getSystemLogger().info("File types that cannot be converted:"+officeType); |
| | | } |
| | | } else { |
| | | SpringMVCContextHolder.getSystemLogger().info("File Not Exist"); |
| | | } |
| | | return outFile; |
| | | } |
| | | /** |
| | | * ææ¡£è½¬æ¢ |
| | | * @param officePath |
| | | * @return |
| | | */ |
| | | public static String OfficeToPdf(String officePath) { |
| | | |
| | | //G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docxâ101951.docxâfeiyu.docx |
| | | String[] split = officePath.split("â"); |
| | | int lastIndex = split[0].lastIndexOf("."); |
| | | int lastNameIndex = split[0].lastIndexOf("/"); |
| | | |
| | | String officeType = split[0].substring(lastIndex+1); |
| | | String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf"; |
| | | String OutPutPath = split[0].substring(0,lastNameIndex+1)+"office/"; |
| | | System.out.println("è¾åºç®å½ï¼"+OutPutPath); |
| | | File file = new File(split[0]); |
| | | File pdfFile = new File(OutPutPath+officeName); |
| | | //夿å½åofficeæä»¶æ¯å¦å·²ç»è½¬ä¸ºPDF,å¦æå·²è½¬ä¸ºPDFå°±ä¸éè¦å次转æ¢ã |
| | | if(pdfFile.exists()){ |
| | | return OutPutPath+officeName; |
| | | } |
| | | |
| | | if (file.exists()) { |
| | | |
| | | double bytes = file.length(); |
| | | double kilobytes = (bytes / 1024); |
| | | double megabytes = (kilobytes / 1024); |
| | | |
| | | DecimalFormat df = new DecimalFormat("0.00"); |
| | | df.setRoundingMode(RoundingMode.HALF_UP); |
| | | String MB = df.format(megabytes); |
| | | Double Size = Double.parseDouble(MB); |
| | | if(Size>10){ |
| | | return Size+"MB"; |
| | | } |
| | | //"doc", "docx", "xls","xlsx", "ppt", "pptx" |
| | | try { |
| | | if(officeType.equals("doc")||officeType.equals("docx")){ |
| | | Word2Pdf(split[0],OutPutPath+"/"+officeName); |
| | | }else if(officeType.equals("xls")||officeType.equals("xlsx")){ |
| | | Excel2Pdf(split[0],OutPutPath+"/"+officeName); |
| | | }else if(officeType.equals("ppt")||officeType.equals("pptx")){ |
| | | PPT2Pdf(split[0],OutPutPath+"/"+officeName); |
| | | }else{ |
| | | System.out.println("æ æ³è¯å«è¯¥æä»¶ï¼"); |
| | | return "Error"; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | return "NotExists"; |
| | | } |
| | | return OutPutPath+officeName; |
| | | } |
| | | |
| | | public static void main(String args[]) { |
| | | String file="E:/work/æ²å´/FE6.6äº§åæ¥ä»·å·¥å
·-æ° - 坿¬.xlsx"; |
| | | AsposeUtil.OfficeToPdf(file); |
| | | } |
| | | } |