word,excel转pdf工具类,含测试类

1.将aspose-cells-8.5.2.jar,aspose-words-15.8.0-jdk16.jar这个两个JAR包引入到项目中:
在这里插入图片描述
2.将license.xml放入resource路径下:
在这里插入图片描述
3.测试方法:
WordToPdfUtil

package com.huaru.utils;

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

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

/**
 * 由于ASPOSE比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms512m -Xmx512m(参考值)<br>
 */
public class WordToPdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {
            InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
     * @param wordFilePath  [原始excel路径]
     * @param pdfFilePath   [输出路径]
     * @return
     */
    public static Boolean wordConvertToPdf(String wordFilePath, String pdfFilePath){
        //验证license
        if (!getLicense()){
            return false;
        }
        try {
            //原始word路径
            Document doc = new Document(wordFilePath);
            //输出路径
            File pdfFile = new File(pdfFilePath);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            doc.save(fileOS, SaveFormat.PDF);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

    public static void main(String[] args) {
        String sourcePath = "G:\\aaa.docx";
        String targetPath = "G:\\test.pdf";
        //验证license
        if (!WordToPdfUtil.getLicense()){
            return;
        }
        long old = System.currentTimeMillis();
        if (WordToPdfUtil.wordConvertToPdf(sourcePath, targetPath)){
            long now = System.currentTimeMillis();
            System.out.println("word转pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
        }
    }

ExcelToPdfUtil

package com.huaru.utils;

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

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

/**
 * excel转pdf工具类
 */
public class ExcelToPdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {
            InputStream is = ExcelToPdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
     */
    public static Boolean excelConvertToPdf(String excelFilePath, String pdfFilePath){
        //验证License
        if (!getLicense()){
            return false;
        }
        try {
            Workbook wb = new Workbook(excelFilePath);
            File pdfFile = new File(pdfFilePath);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            wb.save(fileOS, SaveFormat.PDF);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

 public static void main(String[] args) {
        String sourcePath = "C:\\Users\\caojinlong\\Desktop\\知识图谱工具.xlsx";
        String targetPath = "C:\\Users\\caojinlong\\Desktop\\知识图谱工具.pdf";
        if (!ExcelToPdfUtil.getLicense()){
            return;
        }
        long old = System.currentTimeMillis();
        if (ExcelToPdfUtil.excelConvertToPdf(sourcePath, targetPath)){
            long now = System.currentTimeMillis();
            System.out.println("Excel转Pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
        }
    }

jar包和代码百度网盘提取地址
链接:https://pan.baidu.com/s/1xaA69NUw6-JJRz-_mvyBYA
提取码:yhxn

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值