|
@@ -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,7 +301,70 @@ public class FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Excel 转 PDF(完整缩放到 A4 纸张)
|
|
|
+ *
|
|
|
+ * @param excelPath Excel 文件路径
|
|
|
+ * @param pdfPath 输出 PDF 路径
|
|
|
+ */
|
|
|
+ public static void excelToPdf(String excelPath, String pdfPath, Boolean a) {
|
|
|
+ // 显式指定中文字体
|
|
|
+ FontConfigs.setDefaultFontName("WenQuanYi Micro Hei"); // 文泉驿微米黑
|
|
|
+ FontConfigs.setFontSubstitutes("SimSun", new String[]{"WenQuanYi Micro Hei"}); // 宋体替代
|
|
|
+ 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
|
|
|
*
|
|
@@ -310,6 +372,9 @@ public class FileUtils {
|
|
|
* @param pdfUrl
|
|
|
*/
|
|
|
public static void excelToPdf(String exUrl, String pdfUrl) {
|
|
|
+ // 显式指定中文字体
|
|
|
+ FontConfigs.setDefaultFontName("WenQuanYi Micro Hei"); // 文泉驿微米黑
|
|
|
+ FontConfigs.setFontSubstitutes("SimSun", new String[]{"WenQuanYi Micro Hei"}); // 宋体替代
|
|
|
|
|
|
org.apache.poi.ss.usermodel.Workbook ss = null;
|
|
|
ByteArrayInputStream byteArrayInputStream = null;
|