使用easyPOI的模板功能导出多sheet页的方法

原因:公司里别的部门提供的jar包,接口方法按照传参老是报错,网上也没有详细的解释原因,所以通过debug发现问题的所在,是因为少设置参数。

params.setTemplateUrl("classpath:student_template.xlsx");
        params.setScanAllsheet(false);
        params.setColForEach(true);
        params.setScanAllsheet(true);
        params.setSheetNum(new int[] {0, 1});

如果报错空指针异常,有可能是因为样式未制定,可以设置样式,如下:

 // 设置列宽
    Map<String, ExcelExportEntityColWidth> colWidthMap = new HashMap<>();
    colWidthMap.put("id", new ExcelExportEntityColWidth(15));
    colWidthMap.put("name", new ExcelExportEntityColWidth(20));
    colWidthMap.put("dept", new ExcelExportEntityColWidth(25));
    colWidthMap.put("position", new ExcelExportEntityColWidth(30));

    // 设置样式
    ExcelExportStylerColorImpl styler = new ExcelExportStylerColorImpl();
    styler.setTableHeaderColor((short) 13);
    styler.setTableContentColor((short) 11);
    params.setStyler(styler);

下面是比较完整的流程

public class ExportExcel {
    public static void main(String[] args) {
        // 准备数据模型
        Map<Integer, Map<String, Object>> dataMap = new HashMap<>();
        Map<String, Object> data1 = new HashMap<>();
        data1.put("className", "Class 1");
        List<Student> students1 = new ArrayList<>();
        students1.add(new Student("Tom", 12, "Male"));
        students1.add(new Student("Jerry", 11, "Male"));
        data1.put("students", students1);
        dataMap.put(0, data1);

        Map<String, Object> data2 = new HashMap<>();
        data2.put("className", "Class 2");
        List<Student> students2 = new ArrayList<>();
        students2.add(new Student("Lucy", 13, "Female"));
        students2.add(new Student("Lily", 12, "Female"));
        data2.put("students", students2);
        dataMap.put(1, data2);

        // 准备模板导出参数
        TemplateExportParams params = new TemplateExportParams();
        params.setScanAllsheet(false);
        params.setColForEach(true);
        params.setScanAllsheet(true);
        params.setSheetNum(new int[] {0, 1});

        // 创建Workbook对象并导出Excel
        Workbook workbook = new ExcelExportService().createExcleByTemplate(params, dataMap);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("student_export.xlsx");
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class Student {
    private String name;
    private int age;
    private String gender;

    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    // 省略getter和setter
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
easypoi是一个用于Excel和Word文档操作的Java库。它提供了简单易用的API,可以通过模板导出Excel文件。下面是使用easypoi进行模板导出的示例代码: 1. 导入easypoi的maven坐标: ```xml <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.2.0</version> </dependency> ``` 2. 在Spring Boot的配置文件(bootstrap.yml或application.yml)中配置模板的URL: ```yaml easypoi: template: 'http://www.xxx.cn/statics/template/port.xlsx' ``` 3. 使用easypoi进行模板导出: ```java import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import org.apache.poi.ss.usermodel.Workbook; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; public class TemplateExportDemo { public static void main(String[] args) throws IOException { // 模板文件的URL String templateUrl = "http://www.xxx.cn/statics/template/port.xlsx"; // 下载模板文件 URL url = new URL(templateUrl); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); // 加载模板文件 Workbook workbook = ExcelExportUtil.importExcel(inputStream); // 创建模板参数 TemplateExportParams params = new TemplateExportParams(); params.setSheetNum(0); // 指定导出Sheet // 创建数据模型 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("name", "John"); dataModel.put("age", 25); // 导出Excel文件 FileOutputStream outputStream = new FileOutputStream("output.xlsx"); ExcelExportUtil.exportExcel(params, dataModel, workbook.getSheetAt(params.getSheetNum()), outputStream); // 关闭流 outputStream.close(); inputStream.close(); } } ``` 这段代码会从指定的URL下载模板文件,然后根据模板和数据模型生成新的Excel文件。你可以根据自己的需求修改模板文件和数据模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值