今天接到个需求,要把excel里的多个sheet页导出到一个pdf文件里。拿到这个需求觉得很简单。(评估3天)

1.跳坑

直接用poi笨方法遍历(一共7个excel  每个excel里有7/8个sheet页),如果这么干了 ,可能我现在就不会有时间写这个博客了。

2.再次跳坑

spire.xls.jar   这种方式确实很快。但是,天下没有免费的早中晚饭。只能转3页pdf数据。我的数据太多了 ,不能完全转换。商用(400多),坚决不买。

3.第三次选择正确的方法做正确的事

openOffice  可以操作excel的插件,我选择把excel做成模板

4.希望你们不要想我一样浪费这么长时间。希望帮到诸位!

模板大概长这样,根据实体类传值。

上代码

Module 层

public String getExcelGreat(List<GreatEntrepreneur>  greatList,String templetePath) throws FileNotFoundException, ParsePropertyException, InvalidFormatException{
            try {
                Map<String, Object> beans = new HashMap<String, Object>();
                beans.put("result", greatList);
                XLSTransformer transfer = new XLSTransformer();
                URL url = getClass().getResource(templetePath);
                File file = new File(url.getPath());
                FileInputStream fileInputStream = new FileInputStream(file);
                org.apache.poi.ss.usermodel.Workbook workbook = transfer.transformXLS(fileInputStream, beans);
                String dateStr = GreatEntrepreneur.pathName;
//                String strExcelPath =GreatEntrepreneur.pathUrl;
                String strExcelPath=System.getProperty("java.io.tmpdir");//获取文件临时存储地址
                File excelPath = new File(strExcelPath);
                if (!excelPath.exists()) {
                    excelPath.mkdir();
                }
                String date=getDate("yyyyMMddHHmmss");
                toPdf(workbook,strExcelPath + "/" + dateStr + date +".xlsx");
                if(deleteExcel(strExcelPath + "/" + dateStr + date +".xlsx")){
                    return strExcelPath + "/" + dateStr + date +".pdf";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
    }

public void toPdf(org.apache.poi.ss.usermodel.Workbook workbook,String excelPath){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(excelPath);
            workbook.write(fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            System.out.println("未找到要写的文件");
        } catch (IOException e) {
            System.out.println("写文件是IO异常");
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (Exception e) {
                System.out.println("关闭文件流错误");
            }
        }
        PoiUtil2.openOfficeToPDF(excelPath);
    }

工具类

package com.learn.web.cmap.util;
import java.io.File;
import java.util.Calendar;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

public class PoiUtil2 {
    
    /**
     * @param inputFilePath
     *            源文件路径,如:"e:/test.docx"
     * @return
     */
    public static File openOfficeToPDF(String inputFilePath) {  //1
        return office2pdf(inputFilePath);
    }
 
    /**
     * 根据操作系统的名称,获取OpenOffice.org 4的安装目录<br>
     * @return OpenOffice.org 4的安装目录    */
      //这里需要你自己改,你自己的openoffice.org4 的安装目录
     public static String getOfficeHome() {
        String osName = System.getProperty("os.name");
        System.out.println("操作系统名称:" + osName);
        if (Pattern.matches("Linux.*", osName)) {
            return "/opt/openoffice.org4";
        } else if (Pattern.matches("Windows.*", osName)) {
            return "C:/Program Files (x86)/OpenOffice 4";
        } else if (Pattern.matches("Mac.*", osName)) {
            return "/Applications/OpenOffice.org.app/Contents/";
        }
        return null;
    }
 
    /**
     * 连接OpenOffice.org 并且启动OpenOffice.org
     *
     * @return
     */
    public static OfficeManager getOfficeManager() {
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // 设置OpenOffice.org 4的安装目录
        config.setOfficeHome(getOfficeHome());
        // 启动OpenOffice的服务
        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();
        return officeManager;
    }
 
    /**
     * 转换文件
     *
     * @param inputFile
     * @param outputFilePath_end
     * @param inputFilePath
     * @param
     * @param converter
     */
    public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath,
                                     OfficeDocumentConverter converter) {  //4
        File outputFile = new File(outputFilePath_end);
        // 假如目标路径不存在,则新建该路径
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        converter.convert(inputFile, outputFile);
        System.out.println("文件:" + inputFilePath + "转换为目标文件:" + outputFile + "成功!");
        return outputFile;
    }
 
    /**
     *目标文件路径,如:"e:/test_docx.pdf"
     * @param inputFilePath
     *            源文件路径,如:"e:/test.docx"
     * @param
     * @return
     */
    public static File office2pdf(String inputFilePath) {  //2
        OfficeManager officeManager = null;
        try {
            if (StringUtils.isEmpty(inputFilePath)) {
               System.out.println("输入文件地址为空,转换终止!");
                return null;
            }
 
            File inputFile = new File(inputFilePath);
            // 转换后的文件路径
            String outputFilePath_end = getOutputFilePath(inputFilePath); //3
 
            if (!inputFile.exists()) {
                System.out.println("输入文件不存在,转换终止!");
                return null;
            }
 
            // 获取OpenOffice的安装路劲
            officeManager = getOfficeManager();
            // 连接OpenOffice
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
 
            return converterFile(inputFile, outputFilePath_end, inputFilePath, converter);
        } catch (Exception e) {
        } finally {
            // 停止openOffice
            if (officeManager != null) {
                officeManager.stop();
            }
        }
        return null;
    }
 
    /**
     * 获取输出文件
     * @param inputFilePath
     * @return
     */
    public static String getOutputFilePath(String inputFilePath) {  //3
        String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
        return outputFilePath;
    }
 
    /**
     * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
     *
     * @param inputFilePath
     * @return
     */
    public static String getPostfix(String inputFilePath) {
        return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
    }
 
    public static void main(String[] args) {
     openOfficeToPDF("D:/bfm_exportexcel/1.xlsx");
        
    }

}

大功告成!!!

java操作excel合并sheet 页 导出为pdf相关推荐

  1. java读取sheet2_java读取Excel指定sheet页或全部sheet页数据(含2003和2007版本)

    java读取Excel指定sheet页或全部sheet页数据(含2003和2007版本) 在http://download.csdn.net/detail/u010792467/8072015下载所需 ...

  2. Java Excel 复制单元格 poi Excel 复制单元格 Java Excel 复制行 Java Excel 复制 sheet 页 poi excel copy

    Java Excel 复制单元格 poi Excel 复制单元格 Java Excel 复制行 Java Excel 复制 sheet 页 一.前言 1.本文记录 poi excel 实现 单元格ce ...

  3. java excel导出2007_java操作excel文件,实现批量导出,和导入

    一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...

  4. java mergecells_java怎么操作excel合并单元格

    展开全部 利用java操作Excel,有个开源的东东-jxl.jar,可以到http://sourceforge.net/project/showfiles.php?group_id=79926下载. ...

  5. Java操作Excel并导出

    Java导出Excel表格 提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 Java导出Excel表格 前言 一.企业excel项目导出演示 二.使用步骤 1.引入Mave ...

  6. java横向导出excel_利用Java进行Excel的数据导入导出

    1. 引言 MS 的电子表格(Excel)是Office 的重要成员,是保存统计数据的一种常用格式.在一 个Java 应用中,将一部分数据生成Excel 格式,是与其他系统无缝连接的重要手段.在远程 ...

  7. java操作excel表

    文章分类:Java编程 http://developers.sun.com.cn/blog/functionalca/entry/java读写excel简介 JAVA EXCEL API简介 Java ...

  8. java操作Excel、PDF文件

    java操作Excel.PDF文件 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的java操作excel的工具, 在开源世界中, ...

  9. java excel 导入 格式转换_【转】JAVA实现EXCEL的导入和导出(二)

    =================摘要===================== java如何操作Excel(数据导入导出)(转) jxl.jar,可以到http://sourceforge.net/ ...

最新文章

  1. JS 时间戳转换成日期
  2. python 文字语音朗读-python 利用pyttsx3文字转语音
  3. js/jq基础(日常整理记录)-3-一个自定义表格
  4. hdu1009 - 贪心
  5. Hibernate-HQL
  6. DGL教程【五】使用自己的数据集
  7. Javascript实现浏览器菜单命令
  8. mysql的水平分表和垂直分表的区别
  9. [Leetcode] 第289题 生命游戏
  10. HBase原理和安装
  11. 解决word、excel、ppt文件图标是空白的问题
  12. vue 组件根元素显示优化
  13. 数据分布_数据蒋堂 | 数据分布背后的逻辑
  14. DEM数据基本概念介绍
  15. Visual Assist X V10.7.1929.0 官方原版+破解补丁+破解方法
  16. DeepFaceLab:手动提取高精度脸图,减少抖动!
  17. c语言pl是什么意思,【问答】求助!PL-L PL-S PL-C PL-后面的字母什么意思? - 邦阅网-发现真实的外贸服务商...
  18. java中的switch的参数类型
  19. 大学计算机基础ppt操作题目,键盘操作大学计算机基础.ppt
  20. 我们都需要时间,成为更好的人(转载)

热门文章

  1. 2021年,脸皮厚点儿
  2. iphone4s 如何强制关机
  3. 关于正则表达式的讲解
  4. Ajax请求后防止自动刷新方法
  5. 五一带你找到最省旅游路线【dijkstra算法推导详解】
  6. cytoscape画饼图
  7. Android 9.0 Adaptive Icon 圆形图标剪切不全问题(上下左右部分被裁减)
  8. 游戏更新-五子连珠-Android
  9. 盘点:12种动画制作工具让游戏角色栩栩如生
  10. Spring4Shell Spring CVE-2022-22965漏洞調査