Excel转PDF 一百行代码超简单

不BB直接上代码:https://gitee.com/lanyanhua/excel-to-pdf.git

写了两种情况的应用:上传的文件转PDF。写代码生成的PDF,直接response流输出和本地文件输出


    @PostMapping("/excelToPdf")
    public void excelToPdf(MultipartFile excel, HttpServletResponse response) throws Exception{
        ByteArrayInputStream is = new ByteArrayInputStream(excel.getBytes());
        XSSFWorkbook wb = new XSSFWorkbook(is);
        //修改excel
        //设置输出格式
        response.reset();
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("statements.pdf", "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        //转pdf输出
        ExcelUtil.toPdf(wb,outputStream);
        outputStream.flush();
        outputStream.close();
    }

    @PostMapping("/excelToPdf1")
    public void excelToPdf1(HttpServletResponse response) throws Exception{
        XSSFWorkbook wb = new XSSFWorkbook();
        //修改excel
        XSSFSheet sheet = wb.createSheet();
        XSSFRow row = sheet.createRow(0);
        row.setHeightInPoints(50);
        //设置大小,
        sheet.setColumnWidth(0, 19000);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("我是PDF");
        XSSFCellStyle cellStyle = wb.createCellStyle();
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 20);
        font.setBold(true);
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        //设置输出格式
        response.reset();
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("statements.pdf", "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        //转pdf输出
        //本地改流就好了
        //OutputStream outputStream = new FileOutputStream("/Users/lanyanhua/Desktop/本地文件.pdf");
        ExcelUtil.toPdf(wb,outputStream);
        outputStream.flush();
        outputStream.close();
    }

核心代码



    /**
     * 转pdf
     *
     * @param wb           excel
     * @param outputStream 输出流
     */
    public static void toPdf(XSSFWorkbook wb, OutputStream outputStream) throws Exception {
        // 最后输出
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        com.aspose.cells.Workbook we = new com.aspose.cells.Workbook(is);
        //隐藏workbook中不需要的sheet页。
        printSheetPage(we, new int[]{0});
        //转pdf
        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//        pdfSaveOptions.setOnePagePerSheet(true);
//        pdfSaveOptions.isImageFitToPage(true);
        pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
        //写入 outputStream
        we.save(outputStream, pdfSaveOptions);
        is.close();
        os.close();
        wb.close();
    }

    /**
     * 获取license 去除水印
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void printSheetPage(com.aspose.cells.Workbook wb, int[] page) {
        for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
            wb.getWorksheets().get(i).setVisible(false);
        }
        if (null == page || page.length == 0) {
            wb.getWorksheets().get(0).setVisible(true);
        } else {
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).setVisible(true);
            }
        }
    }


看看效果

 

第二个接口效果

 

最后讲讲代码

这个包是重点aspose-cells-8.5.2.jar maven仓库可能没有方git上了

代码例子在ExcelToPdfApplication 里面

转PDF的在ExcelUtil 里面

生成PDF有啥问题可以试试 PdfSaveOptions 这个类里面的方法,一开始我用的注释的第一行代码就全在一页打印时就全在一页,后面在源码里翻到了现在这个,感觉其他有啥配置应该也是可以设置的

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值