diff --git a/README.md b/README.md index 810b711..22678ff 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ 版权声明 ----------------------------------- 开源不代表免费,科亿遵循 GPL-3.0 开源协议发布,并提供技术交流学习,但 绝不允许修改后和衍生的代码做为闭源的商业软件发布和销售 ! 如果需要将本产品在本地进行任何附带商业化性质行为使用, 请联系项目负责人进行商业授权 ,以遵守 GPL 协议保证您的正常使用。 -目前在国内 GPL 协议具备合同特征,是一种民事法律行为 ,属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。 +目前在国内 GPL 协议具备合同特征,属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。 为什么选择KY KMS? diff --git a/jeecg-boot/jeecg-boot-module-KM/src/main/java/org/jeecg/modules/KM/common/utils/OfficeUtils.java b/jeecg-boot/jeecg-boot-module-KM/src/main/java/org/jeecg/modules/KM/common/utils/OfficeUtils.java index 90a3021..9a3f539 100644 --- a/jeecg-boot/jeecg-boot-module-KM/src/main/java/org/jeecg/modules/KM/common/utils/OfficeUtils.java +++ b/jeecg-boot/jeecg-boot-module-KM/src/main/java/org/jeecg/modules/KM/common/utils/OfficeUtils.java @@ -2,90 +2,83 @@ package org.jeecg.modules.KM.common.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; +import org.apache.poi.hssf.usermodel.HSSFPrintSetup; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.usermodel.PrintSetup; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFPrintSetup; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; /** * * Title: OfficeUtils.java * - * @author zxc + * @author lwx * @time 2018/6/29 下午5:20 */ @Slf4j public class OfficeUtils { -// private static Logger log = LoggerFactory.getLogger(OfficeUtils.class); - - public static boolean convertPdf2(String sofficePath,File srcFile, File targetDir){ - ///Applications/LibreOffice.app/Contents/MacOS/soffice - // --headless --invisible --convert-to - // html --outdir pdf-html/ *.pdf - - Runtime runtime =Runtime.getRuntime(); - StringBuilder command = new StringBuilder(); - command.append(sofficePath); - String outputDir = targetDir.getAbsolutePath(); - command.append(" --headless --invisible --convert-to pdf --outdir "); - command.append(outputDir); - command.append(" "); - command.append(srcFile.getAbsolutePath()); - log.info("开始转换pdf,命令行是:{}",command.toString()); - try { - - ProcessBuilder pb = new ProcessBuilder(command.toString()); - pb.redirectErrorStream(true); - Process process = pb.start(); - printStream(process.getInputStream()); - //Process process = runtime.exec(command.toString()); -// printStream(process.getInputStream()); -// printStream(process.getErrorStream()); - int code = process.waitFor(); - log.info("code:{}",code); - - //String result = IOUtils.toString(process.getInputStream(),"utf-8"); - //String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); - //log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); -// if(StringUtil.isNotEmpty(errorResult)){ -// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult); -// } - File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf"); - log.info("目标文件是:{}",targetFile.getAbsolutePath()); - if(targetFile.exists()){ - log.info("文档转化成功"); - return true; - }else { - log.info("文档转化失败"); - return false; + private static void formatExcel(String srcFile){ + String fileExt = StringUtils.getFileSuffix(srcFile); + if ( fileExt != null ) { + try { + if(fileExt.equals("xlsx")){ + XSSFWorkbook wb = new XSSFWorkbook(Files.newInputStream(Paths.get(srcFile))); + + for (int i = 0; i < wb.getNumberOfSheets(); i++) { + XSSFSheet sheet = wb.getSheetAt(i); + //打印设置 + XSSFPrintSetup print = sheet.getPrintSetup(); + print.setLandscape(true); // 打印方向,true:横向,false:纵向(默认) + print.setFitHeight((short)0);//设置高度为自动分页 + print.setFitWidth((short)1);//设置宽度为一页 + print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型 +// print.setScale((short)55);//自定义缩放①,此处100为无缩放 + //启用“适合页面”打印选项的标志 + sheet.setFitToPage(true); + } + File file = new File(srcFile); + OutputStream fos = new FileOutputStream(file); + wb.write(fos); + }else if(fileExt.equals("xls")){ + POIFSFileSystem fs = new POIFSFileSystem(Files.newInputStream(Paths.get(srcFile))); + HSSFWorkbook wb = new HSSFWorkbook(fs); + + for (int i = 0; i < wb.getNumberOfSheets(); i++) { + Sheet sheet = wb.getSheetAt(i); + //打印设置 + PrintSetup print = sheet.getPrintSetup(); + print.setLandscape(true); // 打印方向,true:横向,false:纵向(默认) + print.setFitHeight((short)0);//设置高度为自动分页 + print.setFitWidth((short)1);//设置宽度为一页 + print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型 + //启用“适合页面”打印选项的标志 + sheet.setFitToPage(true); + } + File file = new File(srcFile); + OutputStream fos = new FileOutputStream(file); + wb.write(fos); + } + } catch (IOException e) { + throw new RuntimeException(e); } - -// if(code==0){ -// log.info("文档转化成功"); -// return true; -// }else { -// log.info("文档转化失败"); -// return false; -// } - } catch (IOException e) { - log.error("convertPdf",e); - } catch (InterruptedException e) { - log.error("convertPdf",e); } - return false; } public static boolean convertPdf(String sofficePath,File srcFile, File targetDir){ - ///Applications/LibreOffice.app/Contents/MacOS/soffice - // --headless --invisible --convert-to - // html --outdir pdf-html/ *.pdf - //Thread.sleep(); + //设置excel文件格式为不换行折断 + formatExcel(srcFile.getAbsolutePath()); Listcommands=new ArrayList<>(); String outputDir = targetDir.getAbsolutePath(); commands.add(sofficePath); @@ -103,40 +96,31 @@ public class OfficeUtils { pb.redirectErrorStream(true); Process process = pb.start(); String result = IOUtils.toString(process.getInputStream(),"utf-8"); - log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); - //printStream(process.getInputStream()); - //Process process = runtime.exec(command.toString()); -// printStream(process.getInputStream()); -// printStream(process.getErrorStream()); - //int code = process.waitFor(); - //log.info("code:{}",code); - process.destroy(); + boolean exitFlag = process.waitFor(10, TimeUnit.SECONDS); + if(exitFlag) { + int code = process.exitValue(); + + log.info("convertPdf from {} output: {}\n result:{}\n exitValue:{}\n", srcFile.getAbsolutePath(), outputDir, result, code); + + process.destroy(); - //String result = IOUtils.toString(process.getInputStream(),"utf-8"); - //String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); - //log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); -// if(StringUtil.isNotEmpty(errorResult)){ -// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult); -// } - File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf"); - log.info("目标文件是:{}",targetFile.getAbsolutePath()); - if(targetFile.exists()){ - log.info("文档转化成功"); - return true; - }else { - log.info("文档转化失败"); + File targetFile = new File(targetDir, StringUtils.getFileNameWithoutSuffix(srcFile.getName()) + ".pdf"); + if (targetFile.exists()) { + log.info("文档转化成功,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath()); + return true; + } else { + log.info("文档转化失败,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath()); + return false; + } + } + else { + log.info("等待命令退出失败"); return false; } - -// if(code==0){ -// log.info("文档转化成功"); -// return true; -// }else { -// log.info("文档转化失败"); -// return false; -// } } catch (IOException e) { - log.error("convertPdf",e); + log.error("convertPdf IOException:",e); + }catch (InterruptedException e) { + log.error("convertPdf InterruptedException:",e); } return false; } @@ -156,6 +140,4 @@ public class OfficeUtils { } }.start(); } - - }