From c0b60dbd5791d499c037c2f7c8cdfc9760353bb3 Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期二, 13 六月 2023 19:29:38 +0800 Subject: [PATCH] 套红文档, --- src/main/java/com/product/file/util/AsposeUtil.java | 552 +++++++++++++++++++++++++++++++++--------------------- 1 files changed, 338 insertions(+), 214 deletions(-) diff --git a/src/main/java/com/product/file/util/AsposeUtil.java b/src/main/java/com/product/file/util/AsposeUtil.java index 152695e..8a0688b 100644 --- a/src/main/java/com/product/file/util/AsposeUtil.java +++ b/src/main/java/com/product/file/util/AsposeUtil.java @@ -8,262 +8,386 @@ import java.math.RoundingMode; import java.text.DecimalFormat; +import com.aspose.cells.Cells; import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; +import com.aspose.cells.Worksheet; +import com.aspose.cells.WorksheetCollection; import com.aspose.slides.Presentation; import com.aspose.words.Document; +import com.aspose.words.ImportFormatMode; +import com.aspose.words.SaveFormat; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; +import com.product.file.config.FileCode; public class AsposeUtil { //鏄惁鍒濆浜唋icense - 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); + private static boolean isInitLicense = false; - isInitLicense=true; - } catch (Exception e) { - result=false; - throw new BaseException(e); - }finally{ - if(is!=null) { + /** + * 鑾峰彇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(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (is != null) { try { is.close(); - }catch(IOException e) { + } 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) { + 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璺緞 + fileOS = new FileOutputStream(OutPutPath); + //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(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { try { fileOS.flush(); fileOS.close(); - }catch(IOException e) { + } 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) { + } + + public static void Excel2Html(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璺緞 + WorksheetCollection sheets = wb.getWorksheets(); + System.out.println("sheet涓暟锛�" + sheets.getCount()); + for (int i = 0; i < sheets.getCount(); i++) { + setAutoWithHeight(sheets.get(i)); + } + fileOS = new FileOutputStream(OutPutPath); + wb.save(fileOS, SaveFormat.HTML); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { try { fileOS.flush(); fileOS.close(); - }catch(IOException e) { + } 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) { + } + + /** + * 璁剧疆鍗曞厓鏍肩殑鑷姩閫傚銆侀珮 + * + * @param sheet + * @throws Exception + */ + public static void setAutoWithHeight(Worksheet sheet) throws Exception { + Cells cells = sheet.getCells(); + int columnCount = cells.getMaxColumn(); //鑾峰彇琛ㄩ〉鐨勬渶澶у垪鏁� + int rowCount = cells.getMaxRow(); //鑾峰彇琛ㄩ〉鐨勬渶澶ц鏁� + if (columnCount < 0 || rowCount < 0) return; + for (int col = 0; col < columnCount; col++) { + sheet.autoFitColumn(col, 0, rowCount); + } + for (int row = 0; row < rowCount; row++) { + sheet.autoFitRow(row, 0, columnCount); + } + } + + /** + * @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.exists()) { + file.mkdirs(); + } + Document doc = new Document(officePath);// 鍘熷word璺緞 + fileOS = new FileOutputStream(OutPutPath); + doc.save(fileOS, com.aspose.words.SaveFormat.PDF); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { try { fileOS.flush(); fileOS.close(); - }catch(IOException e) { + } 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,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� - if (file.exists()&& !"pdf".equals(officeType)) { - double bytes = file.length(); - double kilobytes = (bytes / 1024); - double megabytes = (kilobytes / 1024); + /** + * @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(officePath);// 鍘熷ppt璺緞 + Presentation pres = new Presentation(slides); + fileOS = new FileOutputStream(OutPutPath); + pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { + try { + fileOS.flush(); + fileOS.close(); + } catch (IOException e) { + throw new BaseException(e); + } + } + } + } - 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) { + /** + * 鏂囨。杞崲 + * + * @param officePath + * @param outFile + * @return + * @throws BaseException + */ + public static String OfficeToPdf(String officePath, String outFile, String officeType) throws BaseException { - //G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docx鈱�101951.docx鈱抐eiyu.docx - String[] split = officePath.split("鈱�"); - int lastIndex = split[0].lastIndexOf("."); - int lastNameIndex = split[0].lastIndexOf("/"); + SpringMVCContextHolder.getSystemLogger().info("The file of office type:" + officePath); + if ("pdf".equals(officeType)) { + return outFile; + } + File file = new File(officePath); + //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if (file.exists()) { + if (officeType.equals("doc") || officeType.equals("docx")) { + Word2Pdf(officePath, outFile); + } else if (officeType.equals("xls") || officeType.equals("xlsx")) { +// Excel2Pdf(officePath,outFile); + Excel2Html(officePath, outFile); + } else if (officeType.equals("ppt") || officeType.equals("pptx")) { + PPT2Pdf(officePath, outFile); + } else { + throw new BaseException(FileCode.DOC_CONVERT_NOT_SUPPORT_FILE_FALL); + } + } else { + throw new BaseException(FileCode.DOC_CONVERT_FILE_NOT_EXIST_FALL); + } + return outFile; + } - 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,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� - if(pdfFile.exists()){ - return OutPutPath+officeName; - } + /** + * 鏂囨。杞崲 + * + * @param officePath + * @return + */ + public static String OfficeToPdf(String officePath) { - if (file.exists()) { + String[] split = officePath.split("鈱�"); + int lastIndex = split[0].lastIndexOf("."); + int lastNameIndex = split[0].lastIndexOf("/"); - double bytes = file.length(); - double kilobytes = (bytes / 1024); - double megabytes = (kilobytes / 1024); + 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,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if (pdfFile.exists()) { + return OutPutPath + officeName; + } - 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); - } + 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; + } + + /** + * word鏂囨。鐨勫悎骞� + * + * @param targetWordFile + * @param sourceWordFile + * @return + * @throws Exception + */ + public static File mergeWord(String targetWordFile, String sourceWordFile) throws BaseException { + File target = new File(targetWordFile); + File source = new File(sourceWordFile); + return mergeWord(target, source); + } + + public static void main(String args[]) { + String file = "E:/work/娌勫磧/FE6.6浜у搧鎶ヤ环宸ュ叿-鏂� - 鍓湰.xlsx"; + AsposeUtil.OfficeToPdf(file); + } + + /** + * word鏂囨。鐨勫悎骞� + * + * @param targetWordFile + * @param sourceWordFile + * @return + * @throws Exception + */ + public static File mergeWord(File target, File source) throws BaseException { + + if (!target.exists() && !source.exists()) { + return null; + } else if (!source.exists() && target.exists()) { + return target; + } else if (source.exists() && !target.exists()) { + return source; + } + // 楠岃瘉License + if (!getLicense(2)) { + return null; + } + FileInputStream tin = null; + FileInputStream sin = null; + FileOutputStream out = null; + String temp = target.getParent() + File.separator + "mergeTemp"; + File temp_target = new File(temp + File.separator + target.getName()); + try { + tin = new FileInputStream(target); + sin = new FileInputStream(source); + Document targetDoc = new Document(tin); + Document sourceDoc = new Document(sin); + targetDoc.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); + File dir = new File(temp); + if (!dir.exists()) { + dir.mkdirs(); + } + if (temp_target.createNewFile()) { + out = new FileOutputStream(temp_target); + targetDoc.save(out, SaveFormat.DOCX); + } + } catch (Exception e) { + throw new BaseException(FileCode.DOC_MERGE_FALL, e); + } finally { + try { + if (tin != null) { + tin.close(); + } + if (sin != null) { + sin.close(); + } + if (out != null) { + out.flush(); + out.close(); + } + } catch (IOException e) { + throw new BaseException(FileCode.DOC_MERGE_FALL, e); + } + } + return temp_target; + } } -- Gitblit v1.9.2