Excel转PDF,Excel行数过多导致PDF折行、换行显示

一、导入依赖

 <!--读写Excel-->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
  </dependency>

二、代码

/**
 * excel设置 打印缩放比例
 *
 * @param inputPath  输入文件路径
 * @param outPath    输出文件路径
 */
public static void setExcelScale(String inputPath, String outPath) throws Exception {
    //读取excel文件
    XSSFWorkbook workbook;
    try {
        workbook = new XSSFWorkbook(new FileInputStream(inputPath));
    } catch (FileNotFoundException e) {
        LOG.error("setExcelScale fail: 源文件不存在", e);
        throw new Exception("setExcelScale fail: 源文件不存在");
    } catch (IOException e) {
        LOG.error("setExcelScale fail: 读取源文件IO异常", e);
        throw new Exception("setExcelScale fail: 读取源文件IO异常");
    }
    OutputStream fos = null;
    try {
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
            XSSFSheet sheet = workbook.getSheetAt(i);
            //打印设置
            XSSFPrintSetup print = sheet.getPrintSetup();
            print.setLandscape(true); // 打印方向,true:横向,false:纵向(默认)
            print.setFitHeight((short)0);//设置高度为自动分页
            print.setFitWidth((short)1);//设置宽度为一页
            print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
            //                print.setScale((short)55);//自定义缩放①,此处100为无缩放
            //启用“适合页面”打印选项的标志
            sheet.setFitToPage(true);
        }
        // Excel文件生成后存储的位置。
        File file = new File(outPath);
//            if (!file.exists()){
//                file.createNewFile();
//            }
        fos = new FileOutputStream(file);
        workbook.write(fos);
    } catch (Exception e) {
        LOG.error("setExcelScale fail: 创建输出文件IO异常", e);
        throw new Exception("setExcelScale fail: 创建输出文件IO异常");
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值