word、ppt、excel、txt转pdf——第三篇xls、xlsx

excel转pdf
用到了以下jar,License写在了文中。

<!-- excel工具 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>aspose</groupId>
    <artifactId>cells</artifactId>
    <version>8.5.2</version>
</dependency>

下面上代码

package com.admin.util;

import java.io.ByteArrayInputStream;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import java.io.FileOutputStream;
import java.io.File;

/**
 * Excel转PDF
 */
public class ExcelToPdf {
    /**
     * 获取license
     */
    public static boolean isWordLicense() {
        boolean result = false;
        try {
            String s = "<License>" +
                    "<Data>" +
                    "<Products>" +
                    "<Product>Aspose.Total for Java</Product>" +
                    "<Product>Aspose.Words 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>";
            ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());
            com.aspose.words.License license = new com.aspose.words.License();
            license.setLicense(inputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean ExcelToPdf(String Epath, String Opath) {
        boolean flag = false;
        // 验证License
        if (!isWordLicense()) {
            return false;
        }
        try {
            long start = System.currentTimeMillis();
            Workbook wb = new Workbook(Epath);// 原始excel路径
            File pdfFile = new File(Opath);// 输出路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            wb.save(fileOS, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - start) / 1000.0) + "秒");
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //测试方法
    public static void main(String[] args) {
        ExcelToPdf.ExcelToPdf("D:\\1.xls","D:\\x111.pdf");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值