ソースを参照

后管-在线excel
1、excel生成pdf 代码调整

LHB 4 日 前
コミット
d7dc6afb3f

+ 62 - 3
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/FileUtils.java

@@ -1,7 +1,6 @@
 package org.springblade.manager.utils;
 
-import com.aspose.cells.PdfSaveOptions;
-import com.aspose.cells.SaveFormat;
+import com.aspose.cells.*;
 import com.aspose.cells.Workbook;
 import com.drew.imaging.ImageMetadataReader;
 import com.drew.imaging.ImageProcessingException;
@@ -302,14 +301,74 @@ public class FileUtils {
         }
     }
 
+    /**
+     * Excel 转 PDF(完整缩放到 A4 纸张)
+     *
+     * @param excelPath  Excel 文件路径
+     * @param pdfPath    输出 PDF 路径
+     */
+    public static void excelToPdf(String excelPath, String pdfPath) {
+        Workbook workbook = null;
+        try {
+            // 1. 创建输出目录(如果需要)
+            File pdfFile = new File(pdfPath);
+            File parentDir = pdfFile.getParentFile();
+            if (parentDir != null && !parentDir.exists()) {
+                parentDir.mkdirs();
+            }
 
+            // 2. 直接加载 Excel 文件(无需 Apache POI 中转)
+            workbook = new Workbook(excelPath);
+
+            // 3. 配置 PDF 保存选项
+            PdfSaveOptions pdfOptions = new PdfSaveOptions();
+            pdfOptions.setOnePagePerSheet(true); // 每张表单独一页
+
+            // 4. 遍历所有工作表设置 A4 缩放
+            WorksheetCollection worksheets = workbook.getWorksheets();
+            for (int i = 0; i < worksheets.getCount(); i++) {
+                com.aspose.cells.Worksheet sheet = worksheets.get(i);
+                PageSetup pageSetup = sheet.getPageSetup();
+
+                // 核心缩放设置
+                pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
+                pageSetup.setFitToPagesWide(1);  // 宽度缩放到1页
+                pageSetup.setFitToPagesTall(1);  // 高度缩放到1页
+
+                // 可选优化设置
+//                pageSetup.setOrientation(PageOrientationType.LANDSCAPE); // 横向(宽表格适用)
+                pageSetup.setPrintGridlines(false); // 不打印网格线
+
+                // 调整边距(毫米单位)
+                pageSetup.setLeftMargin(1);
+                pageSetup.setRightMargin(1);
+                pageSetup.setTopMargin(1);
+                pageSetup.setBottomMargin(1);
+            }
+
+            // 5. 保存为 PDF
+            workbook.save(pdfPath, pdfOptions);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            // 6. 释放资源(新版 Aspose.Cells 支持自动关闭)
+            if (workbook != null) {
+                try {
+                    workbook.dispose();
+                } catch (Exception e) {
+                    // 忽略关闭异常
+                }
+            }
+        }
+    }
     /**
      * excel 转pdf
      *
      * @param exUrl
      * @param pdfUrl
      */
-    public static void excelToPdf(String exUrl, String pdfUrl) {
+    public static void excelToPdf(String exUrl, String pdfUrl, Boolean a) {
 
         org.apache.poi.ss.usermodel.Workbook ss = null;
         ByteArrayInputStream byteArrayInputStream = null;