word与excel转pdf的实现

本文介绍了一种使用Aspose库将Word和Excel文件转换为PDF的方法,并提供了无水印、无文件大小限制的转换功能。该方法适用于多种文档格式,包括DOC、DOCX、XLSX等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用aspose实现word与excel转成pdf的功能,附件是myeclipse工程,导入即可运行,生成的pdf文件在与源word或excel文件相同目录下,无水印,无文件大小限制。

资源下载地址:   http://download.youkuaiyun.com/detail/lihong22/9700174

package pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Locale;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;

public class ExeclAndWordToPdf {


    /**
     *  * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转<br>
     *  * 
     *  * @param args
     *
     * @throws Exception   
     */

    public static void main(String[] args) throws Exception {

        String fileName = "";
 
         /*String filePath = "C:\\Users\\Ai\\Desktop\\临时文件\\外包任务推送操作手册.docx";
         String filePathWord = wordToPdf(filePath,fileName);
         System.out.println("word生成pdf文件的路径:"+filePathWord);*/

        String filePath = "C:\\Users\\Ai\\Desktop\\临时文件\\工作簿1.xlsx";

        String filePathExcel = excelToPdf(filePath, fileName);

        System.out.println("excel生成pdf文件的路径:" + filePathExcel);

    }


    /**
     * Excel生成PDF文件
     *  
     *
     * @param filePath
     * @throws Exception
     */

    public static String excelToPdf(String filePath, String fileName) throws Exception {

        // 验证License

        if (!getExcelLicense()) {

            return "Excel license fault!";

        }

        String filePathBorf = filePath.substring(0, filePath.lastIndexOf("\\") + 1);

        if ("".equals(fileName)) {

            fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.indexOf("."));

        }


        long old = System.currentTimeMillis();

        Workbook wb = new Workbook(filePath);// 原始excel路径

        String filePathTemp = filePathBorf + fileName + ".pdf";

        File pdfFile = new File(filePathTemp);// 输出路径

        FileOutputStream fileOS = new FileOutputStream(pdfFile);

        wb.save(fileOS, SaveFormat.PDF);

        long now = System.currentTimeMillis();

        System.out.println("共耗时" + ((now - old) / 1000.0) + "秒");

        return filePathTemp;

    }


    /**
     * word生成PDF文件
     *  
     *
     * @param filePath
     * @throws Exception
     */

    public static String wordToPdf(String filePath, String fileName) throws Exception {

    // 验证License

        if (!getWordLicense()) {

            return "Word license fault!";

        }

        long old = System.currentTimeMillis();

        // 打开文档实例

        Document doc = new Document(filePath);

        String filePathBorf = filePath.substring(0, filePath.lastIndexOf("\\") + 1);

        if ("".equals(fileName)) {

            fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.indexOf("."));

        }

        String filePathTemp = filePathBorf + fileName + ".pdf";

        doc.save(filePathTemp, com.aspose.words.SaveFormat.PDF);

        long now = System.currentTimeMillis();

        System.out.println("共耗时" + ((now - old) / 1000.0) + "秒");

        return filePathTemp;

    }


    /**
     * 获取license
     *  
     *
     * @return
     */

    public static boolean getExcelLicense() {

        boolean result = false;

        try {

            InputStream is = ExeclAndWordToPdf.class.getClassLoader().getResourceAsStream("\\license.xml");

            License aposeLic = new License();

            aposeLic.setLicense(is);

            result = true;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }


    /**
     *  * 获取license
     *  * 
     *  * @return
     *  
     */

    public static boolean getWordLicense() {

        boolean result = false;

        try {

            InputStream is = ExeclAndWordToPdf.class.getClassLoader().getResourceAsStream("\\license.xml");

            com.aspose.words.License aposeLic = new com.aspose.words.License();

            aposeLic.setLicense(is);

            result = true;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值