当前位置:   article > 正文

word、ppt、excel、txt转pdf——第三篇xls、xlsx_xlsx saveformat.pdf

xlsx saveformat.pdf

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

下面上代码

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");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/69529
推荐阅读
相关标签
  

闽ICP备14008679号