ExcelToPdfUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.goafanti.common.utils.pdf;
  2. import com.aspose.cells.License;
  3. import com.aspose.cells.PdfSaveOptions;
  4. import com.aspose.cells.Workbook;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. /**
  8. * Excel转PDF工具类
  9. *
  10. */
  11. public class ExcelToPdfUtils {
  12. /**
  13. * excel 转 pdf
  14. *
  15. * @param excelFilePath excel文件路径
  16. */
  17. public static void excel2pdf(String excelFilePath) {
  18. excel2pdf(excelFilePath, null, null);
  19. }
  20. /**
  21. * excel 转 pdf
  22. *
  23. * @param excelFilePath excel文件路径
  24. * @param convertSheets 需要转换的sheet
  25. */
  26. public static void excel2pdf(String excelFilePath, int[] convertSheets) {
  27. excel2pdf(excelFilePath, null, convertSheets);
  28. }
  29. /**
  30. * excel 转 pdf
  31. *
  32. * @param excelFilePath excel文件路径
  33. * @param pdfFilePath pdf文件路径
  34. */
  35. public static void excel2pdf(String excelFilePath, String pdfFilePath) {
  36. excel2pdf(excelFilePath, pdfFilePath, null);
  37. }
  38. /**
  39. * excel 转 pdf
  40. *
  41. * @param excelFilePath excel文件路径
  42. * @param pdfFilePath pdf文件路径
  43. * @param convertSheets 需要转换的sheet
  44. */
  45. public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
  46. try {
  47. pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
  48. // 验证 License
  49. getLicense();
  50. Workbook wb = new Workbook(excelFilePath);
  51. FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
  52. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
  53. //删除强制每个sheet为一页的设置,允许Aspose自动分页
  54. pdfSaveOptions.setOnePagePerSheet(true);
  55. // 如果有指定需要转换的sheet,隐藏不需要转换的sheet
  56. if (null != convertSheets) {
  57. printSheetPage(wb, convertSheets);
  58. }
  59. // 将文件保存为PDF
  60. wb.save(fileOS, pdfSaveOptions);
  61. fileOS.flush();
  62. fileOS.close();
  63. System.out.println("convert success");
  64. } catch (Exception e) {
  65. System.out.println("convert failed");
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * 获取生成的 pdf 文件路径,默认与源文件同一目录
  71. *
  72. * @param excelFilePath excel文件路径
  73. * @return 生成的 pdf 文件路径
  74. */
  75. public static String getPdfFilePath(String excelFilePath) {
  76. if (excelFilePath == null || excelFilePath.isEmpty()) {
  77. throw new IllegalArgumentException("Excel file path cannot be null or empty");
  78. }
  79. int dotIndex = excelFilePath.lastIndexOf('.');
  80. if (dotIndex > 0) {
  81. return excelFilePath.substring(0, dotIndex) + ".pdf";
  82. }
  83. // 如果文件路径中没有扩展名,直接追加 .pdf
  84. return excelFilePath + ".pdf";
  85. }
  86. /**
  87. * 获取 license 去除水印
  88. * 若不验证则转化出的pdf文档会有水印产生
  89. */
  90. public static void getLicense() {
  91. String licenseFilePath = "excel-license.xml";
  92. try {
  93. InputStream is = ExcelToPdfUtils.class.getClassLoader().getResourceAsStream(licenseFilePath);
  94. License license = new License();
  95. license.setLicense(is);
  96. } catch (Exception e) {
  97. System.out.println("license verify failed");
  98. e.printStackTrace();
  99. }
  100. }
  101. /**
  102. * 隐藏workbook中不需要的sheet页。
  103. *
  104. * @param sheets 显示页的sheet数组
  105. */
  106. public static void printSheetPage(Workbook wb, int[] sheets) {
  107. for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
  108. wb.getWorksheets().get(i).setVisible(false);
  109. }
  110. if (null == sheets || sheets.length == 0) {
  111. wb.getWorksheets().get(0).setVisible(true);
  112. } else {
  113. for (int i = 0; i < sheets.length; i++) {
  114. wb.getWorksheets().get(i).setVisible(true);
  115. }
  116. }
  117. }
  118. public static void main(String[] args) {
  119. // String srcFilePath = Paths.get(FileUtils.getRootPath("fileDir"), "/templates/差旅费报销单打印模板.xlsx").toString();
  120. // ExcelToPdfUtils.excel2pdf(srcFilePath);
  121. ExcelToPdfUtils.excel2pdf("F:\\data\\6-2和6-3 研发支出汇总表和辅助明细账-加计扣除-8.xls");
  122. }
  123. }