Java中最优excel表格转pdf文件实现方案

本文介绍了一个在实际项目开发中如何使用Aspose-cellsJava库解决Excel转PDF过程中遇到的列宽控制和去除水印问题,包括jar包集成、许可证管理及代码实现步骤。
摘要由CSDN通过智能技术生成

在实际项目开发中,通常需要实现excel文件转pdf文件功能,以便于用户能够更好的对文件进行打印和归档。要做好这个功能需要解决以下两个难点问题:

  1. excel列较多,列宽较大时,如何确保转成pdf文件后不被分页?
  2. 使用第三方组件转成pdf文件后,如何确保不带水印?如下图所示:

转成后带水印的pdf文件

下面将采用第三方java包aspose-cells-8.5.2.jar完美解决以上两个问题,具体步骤如下:

1.集成aspose-cells-8.5.2.jar

下载jar包后在项目目录下建一个lib目录,将jar包拷贝其中。如下图所示:

集成aspose-cells-8.5.2.jar包

2.配置pom文件

<dependency>
          <groupId>com.aspose</groupId>
          <artifactId>aspose-cells</artifactId>
          <version>8.5.2</version>
          <scope>system</scope>
          <systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>

3.下载aspose授权文件

下载aspose授权文件,放在如下图目录下:

授权文件存放目录

license.xml授权文件内容如下:

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>      
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>4.编写excel转pdf工具类 

4.编写excel转pdf程序

package com.hellxz.rabbitmq.ssl;

import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

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

public class PdfAsposeUtil {
    public static boolean getLicenseExcel() {
        boolean result = false;
        InputStream is = null;
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
            is = resources[0].getInputStream();
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

   /**
     * excel转pdf
     *
     * @param inpath  excel文件地址
     * @param outPath pdf地址
     */
    public static boolean excel2pdf(String inpath, String outPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicenseExcel()) {
            return false;
        }
        FileOutputStream outputStream = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            outputStream = new FileOutputStream(file);
            Workbook wb = new Workbook(inpath);// 原始excel路径
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放,不分页。
            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(outputStream, pdfSaveOptions);
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }
}

以上是在java中实现excel转pdf文件的详细实现方案。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值