| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.goafanti.common.utils.pdf;
- import com.aspose.cells.License;
- import com.aspose.cells.PdfSaveOptions;
- import com.aspose.cells.Workbook;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- /**
- * Excel转PDF工具类
- *
- */
- public class ExcelToPdfUtils {
- /**
- * excel 转 pdf
- *
- * @param excelFilePath excel文件路径
- */
- public static void excel2pdf(String excelFilePath) {
- excel2pdf(excelFilePath, null, null);
- }
- /**
- * excel 转 pdf
- *
- * @param excelFilePath excel文件路径
- * @param convertSheets 需要转换的sheet
- */
- public static void excel2pdf(String excelFilePath, int[] convertSheets) {
- excel2pdf(excelFilePath, null, convertSheets);
- }
- /**
- * excel 转 pdf
- *
- * @param excelFilePath excel文件路径
- * @param pdfFilePath pdf文件路径
- */
- public static void excel2pdf(String excelFilePath, String pdfFilePath) {
- excel2pdf(excelFilePath, pdfFilePath, null);
- }
- /**
- * excel 转 pdf
- *
- * @param excelFilePath excel文件路径
- * @param pdfFilePath pdf文件路径
- * @param convertSheets 需要转换的sheet
- */
- public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
- try {
- pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
- // 验证 License
- getLicense();
- Workbook wb = new Workbook(excelFilePath);
- FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
- PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
- //删除强制每个sheet为一页的设置,允许Aspose自动分页
- pdfSaveOptions.setOnePagePerSheet(true);
- // 如果有指定需要转换的sheet,隐藏不需要转换的sheet
- if (null != convertSheets) {
- printSheetPage(wb, convertSheets);
- }
- // 将文件保存为PDF
- wb.save(fileOS, pdfSaveOptions);
- fileOS.flush();
- fileOS.close();
- System.out.println("convert success");
- } catch (Exception e) {
- System.out.println("convert failed");
- e.printStackTrace();
- }
- }
- /**
- * 获取生成的 pdf 文件路径,默认与源文件同一目录
- *
- * @param excelFilePath excel文件路径
- * @return 生成的 pdf 文件路径
- */
- public static String getPdfFilePath(String excelFilePath) {
- if (excelFilePath == null || excelFilePath.isEmpty()) {
- throw new IllegalArgumentException("Excel file path cannot be null or empty");
- }
- int dotIndex = excelFilePath.lastIndexOf('.');
- if (dotIndex > 0) {
- return excelFilePath.substring(0, dotIndex) + ".pdf";
- }
- // 如果文件路径中没有扩展名,直接追加 .pdf
- return excelFilePath + ".pdf";
- }
- /**
- * 获取 license 去除水印
- * 若不验证则转化出的pdf文档会有水印产生
- */
- public static void getLicense() {
- String licenseFilePath = "excel-license.xml";
- try {
- InputStream is = ExcelToPdfUtils.class.getClassLoader().getResourceAsStream(licenseFilePath);
- License license = new License();
- license.setLicense(is);
- } catch (Exception e) {
- System.out.println("license verify failed");
- e.printStackTrace();
- }
- }
- /**
- * 隐藏workbook中不需要的sheet页。
- *
- * @param sheets 显示页的sheet数组
- */
- public static void printSheetPage(Workbook wb, int[] sheets) {
- for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
- wb.getWorksheets().get(i).setVisible(false);
- }
- if (null == sheets || sheets.length == 0) {
- wb.getWorksheets().get(0).setVisible(true);
- } else {
- for (int i = 0; i < sheets.length; i++) {
- wb.getWorksheets().get(i).setVisible(true);
- }
- }
- }
- public static void main(String[] args) {
- // String srcFilePath = Paths.get(FileUtils.getRootPath("fileDir"), "/templates/差旅费报销单打印模板.xlsx").toString();
- // ExcelToPdfUtils.excel2pdf(srcFilePath);
- ExcelToPdfUtils.excel2pdf("F:\\data\\6-2和6-3 研发支出汇总表和辅助明细账-加计扣除-8.xls");
- }
- }
|