Java利用aspose.cells对xlsx文件转PDF

本文需要aspose.cells.License

完整代码放在最后

第一步,导入Maven依赖和包

<dependency>  
    <groupId>com.aspose</groupId>  
    <artifactId>aspose-cells</artifactId>  
    <version>9.0.0</version>  
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

第二步,导入验证许可函数

    public static boolean cellsgetLicense() {
        boolean result = false;
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            license = loader.getResourceAsStream("cellslicense.xml"); // 导入许可
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

第三步,设置好输入输出的路径变量

    private static InputStream license;
    private static InputStream fileInput; // 输入路径
    private static File outputFile;  // 输出路径

第四步,转换函数

public static void main(String[] args) {
        // 获取License
        if (!cellsgetLicense()) {
            return;
        }
        try {
            fileInput = new FileInputStream("read_from_this.xlsx"); // 待处理的文件路径
            outputFile = new File("E:\\导出文件\\test.pdf"); // 输出路径
            Workbook wb = new Workbook(fileInput);
            FileOutputStream fileOS = new FileOutputStream(outputFile);
            wb.save(fileOS, SaveFormat.PDF);
            System.out.println("文件保存在:" + outputFile.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

完整代码

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

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

public class test {
    private static InputStream license;
    private static InputStream fileInput; // 输入路径
    private static File outputFile;  // 输出路径

    public static boolean cellsgetLicense() {
        boolean result = false;
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            license = loader.getResourceAsStream("cellslicense.xml"); // 导入许可
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    public static void main(String[] args) {
        // 获取License
        if (!cellsgetLicense()) {
            return;
        }
        try {
            fileInput = new FileInputStream("read_from_this.xlsx"); // 待处理的文件路径
            outputFile = new File("E:\\导出文件\\test.pdf"); // 输出路径
            Workbook wb = new Workbook(fileInput);
            FileOutputStream fileOS = new FileOutputStream(outputFile);
            wb.save(fileOS, SaveFormat.PDF);
            System.out.println("文件保存在:" + outputFile.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值