Java aspose 转换使用

package com.example.module.web;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.words.Document;

import java.io.*;

/**

  • excel转换为pdf的工具类

  • @author shmily
    */
    public class Excel2PdfUtil {

    /**

    • 许可证字符串
      */
      private static final String LICENSE = “” +
      “” +
      “Aspose.Total for JavaAspose.Words for Java” +
      “Enterprise” +
      “20991231” +
      “20991231” +
      “8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7” +
      “” +
      “sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=” +
      “”;

    /**

    • 设置 license 去除水印
      */
      private static void setLicense() {
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(LICENSE.getBytes());
      License license = new License();
      license.setLicense(byteArrayInputStream);
      }

    /**

    • excel 转 pdf
    • @param excelFilePath excel文件路径
    • @param pdfFilePath pdf文件路径
    • @param convertSheets 需要转换的sheet
      */
      public static void excelConvertPdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
      FileOutputStream fileOutputStream = null;
      try {
      pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
      // 设置License
      getLicense(“excel-license.xml”);
      // 读取excel文件
      Workbook wb = new Workbook(excelFilePath);
      fileOutputStream = new FileOutputStream(pdfFilePath);
      // 设置pdf格式
      PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
      pdfSaveOptions.setOnePagePerSheet(true);
      if (null != convertSheets) {
      printSheetPage(wb, convertSheets);
      }
      wb.save(fileOutputStream, pdfSaveOptions);
      fileOutputStream.flush();
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      try {
      assert fileOutputStream != null;
      fileOutputStream.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }

    /**

    • excel 转 pdf 二进制流
    • @param excelFilePath excel文件路径
    • @param pdfFilePath pdf文件路径
    • @param convertSheets 需要转换的sheet
      */
      public static void excelConvertPdfByte(String excelFilePath, String pdfFilePath, int[] convertSheets) {
      FileOutputStream fileOutputStream = null;
      try {
      pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
      // 设置License
      setLicense();
      // 读取excel文件
      Workbook wb = new Workbook(excelFilePath);
      fileOutputStream = new FileOutputStream(pdfFilePath);
      // 设置pdf格式
      PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
      pdfSaveOptions.setOnePagePerSheet(true);
      if (null != convertSheets) {
      printSheetPage(wb, convertSheets);
      }
      wb.save(fileOutputStream, pdfSaveOptions);
      fileOutputStream.flush();
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      try {
      assert fileOutputStream != null;
      fileOutputStream.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }

    /**

    • 获取 生成的 pdf 文件路径,默认与源文件同一目录
    • @param excelPath excel文件
    • @return 生成的 pdf 文件
      */
      private static String getPdfFilePath(String excelPath) {
      int lastIndexOfPoint = excelPath.lastIndexOf(“.”);
      String pdfFilePath = “”;
      if (lastIndexOfPoint > -1) {
      pdfFilePath = excelPath.substring(0, lastIndexOfPoint);
      }
      return pdfFilePath + “.pdf”;
      }

    /**

    • 隐藏workbook中不需要的sheet页。
    • @param sheets 显示页的sheet数组
      */
      private static void printSheetPage(Workbook wb, int[] sheets) {
      // 隐藏非第一个sheet页
      for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
      wb.getWorksheets().get(i).setVisible(false);
      }
      // 设置显示的sheet页
      if (null == sheets || sheets.length == 0) {
      wb.getWorksheets().get(0).setVisible(true);
      } else {
      for (int i = 0; i < sheets.length; i++) {
      wb.getWorksheets().get(i).setVisible(true);
      }
      }
      }

    public static void ppt2Pdf(String inPath,String outPath){
    // 验证License 去除水印
    getLicense(“license.xml”);
    long start = System.currentTimeMillis();
    try {
    FileInputStream fileInput = new FileInputStream(inPath);
    Presentation pres = new Presentation(fileInput);
    FileOutputStream out = new FileOutputStream(new File(outPath));
    pres.save(out, SaveFormat.Pdf);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    long end =System.currentTimeMillis();
    }

    /**

    • @param inPath 源文件路径
    • @param outPath 新pdf文件路径
      */
      public static void doc2pdf(String inPath, String outPath) {
      // 验证License 去除水印
      getLicense(“word-license.xml”);
      File file = new File(outPath);
      if(file.exists()){
      file.delete();
      }
      FileOutputStream os = null;
      try {
      // 新建一个空白pdf文档
      os = new FileOutputStream(file);
      // Address是将要被转化的word文档
      Document doc = new Document(inPath);
      // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
      doc.save(os, com.aspose.words.SaveFormat.PDF);
      // EPUB, XPS, SWF 相互转换
      } catch (Exception e) {
      e.printStackTrace();
      }
      finally {
      try {
      os.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }

    public static boolean getLicense(String xmlFile) {
    boolean result = false;
    try {
    // license.xml应放在
    InputStream is = Excel2PdfUtil.class.getClassLoader().getResourceAsStream(xmlFile);
    License aposeLic = new License();
    aposeLic.setLicense(is);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
    }

    public static void main(String[] args) {
    // Excel2PdfUtil.excelConvertPdf(“F:\Excel01.xls”, “F:\Excel01.pdf”, null);
    // Excel2PdfUtil.ppt2Pdf(“F:\测试111.pptx”, “F:\测试111.pdf”);
    Excel2PdfUtil.doc2pdf(“F:\123.docx”, “F:\123.pdf”);
    }
    }

aspose aspose-cells 8.5.2
    <dependency>
        <groupId>aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>15.8.0</version>
    </dependency>
    <!-- ppt转pdf -->
    <dependency>
        <groupId>aspose</groupId>
        <artifactId>aspose.slides</artifactId>
        <version>19.3</version>
    </dependency>
  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值