使用aspose相关包将excel转成pdf 并导出

这篇具有很好参考价值的文章主要介绍了使用aspose相关包将excel转成pdf 并导出。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SpringBoot 项目 基于aspose相关jar包 将excel 转换成pdf 导出

1、依赖的jar包 , jar获取链接 aspose相关三方jar ,下载解压后,在项目路径下建一个libs包,然后将下图两个jar 拷贝至刚新建的libs目录中

使用aspose相关包将excel转成pdf 并导出,excel,pdf文章来源地址https://www.toymoban.com/news/detail-845140.html

2、pom.xml中加入maven引入

        <dependency>
            <groupId>com.aspose.cells</groupId>
            <artifactId>cells-8.5.2 </artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/libs/aspose-cells-8.5.2.jar</systemPath>
            <version>8.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.aspose.words</groupId>
            <artifactId>words-15.8.0 </artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/libs/aspose-words-15.8.0.jar</systemPath>
            <version>15.8.0</version>
        </dependency>
2.1 使用SpringBoot打包插件生成jar包的时候,你会发现这个jar包不会被打进去,进而出现错误。解决这个问题就需要在maven打包插件中配置一个includeSystemScope属性
<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--设置为true,以便把本地的system的jar也包括进来-->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

3、编写转换工具类 如下

package com.by.excelToPdf;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class PdfUtil {

    /**
     * excel 转 pdf
     * @param is 输入流
     * @return 输出流
     */
    public static ByteArrayOutputStream excel2pdf(ByteArrayInputStream is) {
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            // 验证 License
            getLicense();
            Workbook wb = new Workbook(is);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(bos, pdfSaveOptions);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            System.out.println("convert failed");
            e.printStackTrace();
        }
        return bos;
    }

    /**
     * excel 转 pdf
     *
     * @param excelFilePath excel文件路径
     */
    public static void excel2pdf(String excelFilePath) {
        excel2pdf(excelFilePath, null, null);
    }

    /**
     * excel 转 pdf
     *
     * @param excelFilePath excel文件路径
     * @param convertSheets 需要转换的sheet
     */
    public static void excel2pdf(String excelFilePath, int[] convertSheets) {
        excel2pdf(excelFilePath, null, convertSheets);
    }

    /**
     * excel 转 pdf
     *
     * @param excelFilePath excel文件路径
     * @param pdfFilePath   pdf文件路径
     */
    public static void excel2pdf(String excelFilePath, String pdfFilePath) {
        excel2pdf(excelFilePath, pdfFilePath, null);
    }

    /**
     * excel 转 pdf
     *
     * @param excelFilePath excel文件路径
     * @param pdfFilePath   pdf文件路径
     * @param convertSheets 需要转换的sheet
     */
    public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
        try {
            pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
            // 验证 License
            getLicense();
            Workbook wb = new Workbook(excelFilePath);
            FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            if (null != convertSheets) {
                printSheetPage(wb, convertSheets);
            }
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
            System.out.println("convert success");
        } catch (Exception e) {
            System.out.println("convert failed");
            e.printStackTrace();
        }
    }

    /**
     * 获取 生成的 pdf 文件路径,默认与源文件同一目录
     *
     * @param excelFilePath excel文件
     * @return 生成的 pdf 文件
     */
    private static String getPdfFilePath(String excelFilePath) {
        return excelFilePath.split("\\.")[0] + ".pdf";
    }

    /**
     * 获取 license 去除水印
     * 若不验证则转化出的pdf文档会有水印产生
     */
    private static void getLicense() {
        String licenseFilePath = "excel-license.xml";
        try {
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);
            License license = new License();
            license.setLicense(is);
        } catch (Exception e) {
            System.out.println("license verify failed");
            e.printStackTrace();
        }
    }

    /**
     * 隐藏workbook中不需要的sheet页。
     *
     * @param sheets 显示页的sheet数组
     */
    private static void printSheetPage(Workbook wb, int[] sheets) {
        for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
            wb.getWorksheets().get(i).setVisible(false);
        }
        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);
            }
        }
    }
}

4、调用 工具类中有基于流的方式入参、文件地址方式入参等,大家可根据自行需要选择合适的转换方法

    public static void main(String[] args) {
        String inputFile = "D:/testPdf/222.xlsx";
        String outputFile = "D:/testPdf/222.pdf";
        PdfUtil.excel2pdf(inputFile, outputFile);
    }

5、注意问题,以上转换在windows环境运行一切正常,可能部署到linux环境会存在中文乱码,引起乱码的原因可能是因为linux环境无中文相关字体

  • linux环境查看字段方法 字体路径/usr/share/fonts
# 刷新字体缓存
fc-cache
# 查看所有字体
fc-list
# 查看所有中文字体
fc-list :lang=zh
  • 如果无中文字体 我们可能把windows环境中的字段上传至linux字段目录下,windows环境字段路径C:\Windows\Fonts,上传后安装字段
yum -y install mkfontscale mkfontdir fontconfig
# mkfontscale:字体扩展、mkfontdir:新增字体目录、fc-cache:刷新缓存
mkfontscale && mkfontdir && fc-cache 
  • 如果使用docker 容器启动的应用服务,则还需要使用挂载卷的方式,将宿主体的字体和容器共享,具体方式即启动容器时 加上 “-v /usr/share/fonts/:/usr/share/fonts”

到了这里,关于使用aspose相关包将excel转成pdf 并导出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【工具插件类教学】Unity通过Aspose读取并显示打开PDF,PPT,Excel,Word

    目录 一、获取Aspose支持.Net的DLL 二、导入Unity的Plugin文件夹 三、分别编写四种文件的读取显示

    2024年02月02日
    浏览(42)
  • js将Excel转成PDF(纸张、分页、铺满、提高清晰度)

    依赖: 示例html DOM: 核心js: - - -

    2024年02月11日
    浏览(28)
  • aspose-words、itextpdf完美解决java将word、excel、ppt、图片转换为pdf文件

    我是 傲骄鹿先生 ,沉淀、学习、分享、成长。 如果你觉得文章内容还可以的话,希望不吝您的「一键三连」,文章里面有不足的地方希望各位在评论区补充疑惑、见解以及面试中遇到的奇葩问法 面对日常开发过程中,将各种文件转换为pdf文件的问题,总是让人头疼,这次终

    2024年02月03日
    浏览(42)
  • Aspose.cell excel转pdf日期格式不正确yyyy/MM/dd变成MM/dd/yyyy

    最近使用Aspose.cell将excel转pdf过程中excel中时间格式列的显示和excel表里的值显示不一样。 excel里日期格式 yyyy/MM/dd pdf里日期格式MM/dd/yyyy 主要原因:linux和windows里内置的时间格式不一致,当代码部署到linux服务器的时候转换格式就会发生不一致的问题。 解决方法:使用apache p

    2024年02月15日
    浏览(29)
  • 文档在线预览(四)将word、txt、ppt、excel、图片转成pdf来实现在线预览

    @ 目录 事前准备 1、需要的maven依赖 添加spire依赖(商用,有免费版,但是存在页数和字数限制,不采用spire方式可不添加) 2、后面用到的工具类代码: 一、word文件转pdf文件(支持doc、docx) 1、使用aspose方式 2、使用poi方式 3、使用spire方式 二、txt文件转pdf文件 三、PPT文件转

    2024年02月08日
    浏览(76)
  • Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理

    感谢小伙伴儿的分享: ● 不羁 ● 郭中天 整合调整后的工具类Gitee地址:https://gitee.com/yuanzhengme/java_application_aspose_demo ● WordToPdfUtil用于将word文档转换为pdf格式的工具类 ● ExcelToPdfUtil用于将excel文档转换为pdf格式的工具类 ● PdfToImageUtil用于将pdf文档转换为image格式的工具类

    2024年01月24日
    浏览(58)
  • Aspose导出word使用记录

    背景 :Aspose系列的控件,功能实现都比较强大,可以实现多样化的报表设计及输出。 通过这次业务机会,锂宝碳审核中业务功需要实现Word文档表格的动态导出功能,因此学习了相关内容,在学习和参考了官方API文档的帮助下,将学习和简单的使用记录在wiki中。下面由我来简

    2024年02月10日
    浏览(40)
  • Aspose.Pdf使用教程:在PDF文件中添加水印

    Aspose.PDF  是一款高级PDF处理API,可以在跨平台应用程序中轻松生成,修改,转换,呈现,保护和打印文档。无需使用Adobe Acrobat。此外,API提供压缩选项,表创建和处理,图形和图像功能,广泛的超链接功能,图章和水印任务,扩展的安全控件和自定义字体处理。本文将为你

    2024年02月01日
    浏览(51)
  • 用Aspose-Java免费实现 PDF、Word、Excel、Word互相转换并将转换过得文件上传OSS,返回转换后的文件路径

    github代码地址 https://github.com/Tom-shushu/work-study 接口文档有道云 https://note.youdao.com/s/GShGsYE8 接口文档离线版本 https://files.cnblogs.com/files/Tom-shushu/%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.rar?t=1682958343download=true 为什么发布这篇文档转换的文章呢?因为上周我要将一个PDF转换为Word,结果百度谷歌

    2024年02月02日
    浏览(38)
  • Java + React导出Excel/PDF

    前言 在B/S架构中,服务端导出是一种高效的方式。它将导出的逻辑放在服务端,前端仅需发起请求即可。通过在服务端完成导出后,前端再下载文件完成整个导出过程。服务端导出具有许多优点,如数据安全、适用于大规模数据场景以及不受前端性能影响等。 本文将使用前

    2024年02月10日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包