栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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

    
    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;


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

    
    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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号