aspose-cells将Excel转为pdf

使用aspose-cells8.5.2将Excel转为pdf,实现在线预览。无水印。

下载地址:https://download.csdn.net/download/qq_31674229/32457846

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * 将Excel转换为pdf
 */
public class Excel2PdfUtil {
    private static Logger logger = LoggerFactory.getLogger(Excel2PdfUtil.class);

    public static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            is = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("license-cells.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            if (is != null){
                License aposeLic = new License();
                aposeLic.setLicense(is);
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(is != null){
                    is.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将Excel转换为pdf
     * @param inPath excel存储路径
     * @param outPath pdf保存路径
     */
    public static void excel2pdf(String inPath, String outPath) {
        if (!getLicense() || StringUtils.isEmpty(inPath) || StringUtils.isEmpty(outPath)) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Workbook wb = new Workbook(inPath); // Address是将要被转化的excel文档
            // 设置pdf保存的格式以及强制所有列都在同一页
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);

            int sheetCount = wb.getWorksheets().getCount();
            int[] showSheets=new int[sheetCount];
            for (int i = 0; i < sheetCount; i++) {
                showSheets[i]=i;
            }
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
            //autoDraw(wb,showSheets);
            //隐藏workbook中不需要的sheet页。
            printSheetPage(wb,showSheets);
            wb.save(os, pdfSaveOptions);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换

            os.flush();
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("Excel转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 将excel转为pdf
     * @param input 需要转换的word
     * @param saveFile 保存pdf文件
     */
    public static void excel2pdf(InputStream input, File saveFile) {
        if (!getLicense() || input==null || saveFile==null) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(saveFile);
            Workbook wb = new Workbook(input); // Address是将要被转化的excel文档
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);

            int sheetCount = wb.getWorksheets().getCount();
            int[] showSheets=new int[sheetCount];
            for (int i = 0; i < sheetCount; i++) {
                showSheets[i]=i;
            }
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
            autoDraw(wb,showSheets);
            //隐藏workbook中不需要的sheet页。
            printSheetPage(wb,showSheets);
            wb.save(os, pdfSaveOptions);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换

            os.flush();
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("Excel转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置打印的sheet 自动拉伸比例
     * @param wb
     * @param page 自动拉伸的页的sheet数组
     */
    private static void autoDraw(Workbook wb,int[] page){
        if(null!=page&&page.length>0){
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
                wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
            }
        }
    }


    /**
     * 隐藏workbook中不需要的sheet页。
     * @param wb
     * @param page 显示页的sheet数组
     */
    private static void printSheetPage(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);
            }
        }
    }

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值