Easy POI 导入excel

参考:https://gitee.com/lemur/easypoi?_from=gitee_search
POI 工具类,Excel的快速导入导出,Excel模板导出,Word模板导出,可以仅仅5行代码就可以完成Excel的导入导出,修改导出格式简单粗暴,快速有效

依赖

        <!-- Easy POI 依赖 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.3</version>
        </dependency>

在需要导出的实体类上加注解

	......
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
    @Excel(name = "出生日期",width = 20,format = "yyyy-MM-dd")//导出到excel的列名 设置成宽度20 格式年月日
    private LocalDate birthday;

    @Excel(name = "身份证号",width = 30)//导出到excel的列名 设置成宽度30
    private String idCard;
    
    @Excel(name = "合同期限",suffix = "年")//后面加上年
    private Double contractTerm;

    @TableField(exist = false)
    @ExcelEntity(name = "民族")//本来的字段是id,指定这个是实体类
    private Nation nation;
	//-----------------和以上那个民族对应使用---------------------------
    @Excel(name = "民族")
    @NonNull
    private String name;
    //----------------对应民族实体的字段--------------------------------
    ......
导出代码
```javascript
    @GetMapping(value = "/export", produces = "application/octet-stream")//流形式,防止乱码
    public void exportEmployee(HttpServletResponse response) {
    
    
        //获取所有员工信息集合
        List<Employee> employees = employeeService.getEmployee(null);
        //导出参数
        ExportParams exportParams = new ExportParams("员工表", "员工表", ExcelType.HSSF);//文件名:员工表,sheetName:员工表,HSSF:03版excel的
        //导出工具类,得到工作簿workbook
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Employee.class, employees);
        //输出流
        ServletOutputStream outputStream = null;
        try {
    
    
            // 流形式,设置响应头
            response.setHeader("content-type", "application/octet-stream");
            // 防止中文乱码
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("员工表.xls", "UTF-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (null != outputStream) {
    
    
                try {
    
    
                    outputStream.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_55806809/article/details/121501713