java excel转pdf

这篇博客介绍了如何在Java项目中使用Aspose库将Excel文件转换为PDF,包括添加Aspose依赖、设置字体避免乱码、以及如何处理Aspose的许可证。同时,提供了包含破解XML的示例代码,帮助开发者解决可能遇到的水印问题。请注意,使用破解软件可能违反授权规定。
摘要由CSDN通过智能技术生成

添加aspose依赖

aspose.cells依赖下载下来,本地导入进pom,尽量放在项目文件夹里

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose</artifactId>
	<version>8.5.2</version>
	<scope>system</scope
	<systemPath>${pom.basedir}/src/main/resources/templates/aspose-cells-20.7-crack.jar</systemPath>
</dependency>

excel转pdf代码

package com.zhenghe.api.manager.service.exportData;

import com.aspose.cells.*;
import org.springframework.stereotype.Service;
import java.io.*;

@Service
public class PdfService {
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = PdfService.class.getClassLoader().getResourceAsStream("templates/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * excel 转为pdf 输出。
     *
     * @param sourceFilePath  excel文件
     * @param desFilePathd  pad 输出文件目录
     */
    public static void excel2pdf(String sourceFilePath, String desFilePathd ){
        if (!getLicense()) { // 验证License 不然会有水印
            return;
        }
        try {
        	//aspose必须要20以上的版本不然会报错,说没有IndividualFontConfigs这个类
        	//设置字体,如果不把本地的字体上传到服务的话。出来的pdf会乱码,
            IndividualFontConfigs configs = new IndividualFontConfigs();
            configs.setFontFolder("/usr/share/fonts/chinese", true);
            LoadOptions loadOptions  = new LoadOptions();
            loadOptions.setFontConfigs(configs);
            Workbook wb = new Workbook(sourceFilePath,loadOptions);// 原始excel路径

            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);


            int[] autoDrawSheets={3};

            int[] showSheets={0};
            printSheetPage(wb,showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    


    
    public static void printSheetPage(Workbook wb,int[] page){
        for (int i= 1; i < wb.getWorksheets().getCount(); i++)  {
            wb.getWorksheets().get(i).setVisible(false);
        }
        if(null==page||page.length==0){
            wb.getWorksheets().get(0).setVisible(true);
        }else{
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).setVisible(true);
            }
        }
    }
}

aspose破解xml

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

##例外
如果打包到服务器上报错运行不起来
请按以下操作
图片: 在这里插入图片描述
然后正在进行打包,说不定就可以了,我也是稀里糊涂的就可以了
可能是我重启过电脑,在打包的

资源

java excel转pdf 所需要文件

经供参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值