SpringBoot集成阿里easyexcel(一)基础导入导出

SpringBoot集成阿里easyexcel(一)基础导入导出

easyexcel主要用于excel文件的读写,可使用model实体类来定义文件读写的模板,对开发人员来说实现简单Excel文件的读写很便捷。可参考官方文档 https://github.com/alibaba/easyexcel

一、引入依赖

 <!-- 阿里开源EXCEL -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>${easyexcel.version}</version>
        </dependency>

文档地址:https://www.yuque.com/easyexcel/doc/easyexcel

二、实体

普通导出实体,index的数字为导出的列,为0开始

@Data
public class ProjectExpView {
    @ExcelProperty(value = "项目编号",index = 0)
    private String projectCode;
    @ExcelProperty(value = "项目名称",index = 1)
    private String projectName;
}

合并单元格导出实体

@Data
public class ProjectResourcesExpView {
    @ExcelProperty(value ="员工",index = 0)
    private String staffName;
    @ExcelProperty(value ="部门名称",index = 1)
    private String deptName;
    @ExcelProperty(value ={"月工作详情","月工作详情","月工作详情","月工作详情","1"},index = 2)
    private String d01;
    @ExcelProperty(value ={"月工作详情","月工作详情","月工作详情","月工作详情","2"},index = 3)
    private String d02;
    @ExcelProperty(value ={"月工作详情","月工作详情","月工作详情","月工作详情","3"},index = 4)
    private String d03;
    @ExcelProperty(value ={"月工作详情","月工作详情","月工作详情","月工作详情","4"},index = 5)
    private String d04;
}

导出样式
在这里插入图片描述设置行高:@ContentRowHeight(150)作用在类上
设置列宽: @ColumnWidth(25)作用在字段上
忽略导出字段:@ExcelIgnore
设置时间字段导出格式: @DateTimeFormat(“yyyy-MM-dd”)

三、导出

根据查询出来的列表信息导出到页面

    @GetMapping("/export")
    @ApiOperation("导出XXX信息")
    public void exportProjectExpView(HttpServletResponse response, HttpServletRequest request) throws IOException {
        List<ProjectExpView> list = ProjectService.exportList();
        String name = "XXX信息";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String(name.getBytes("gbk"), StandardCharsets.ISO_8859_1)  + ".xlsx");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(0, name).head(SyfwEwmExport.class).build();
        excelWriter.write(list, writeSheet1);

        excelWriter.finish();
    }

四、导入

 @PostMapping("/import")
    @ApiOperation("导入XX信息")
    public ResponseResult<?> importProject(@RequestParam("file") MultipartFile file)  throws Exception{
        List<ProjectExpView> list = new ArrayList<>(1);
        List<ImportErrVo> errMsgList = new ArrayList<>(1);
        ExcelListener excelListener = new ExcelListener();
        Object Object1 = ExcelUtil.readExcel(file,ProjectExpView.class,0,excelListener);
        list = (List<ProjectExpView>) Object1;
        projectService.importProject(list);
        return ResponseResult.importSuccess();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于使用Spring BootEasyExcel来实现Excel导入导出,你可以按照以下步骤进行操作: 1. 添加依赖:在你的Spring Boot项目的pom.xml文件中添加EasyExcel的依赖。 ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.11</version> </dependency> ``` 2. 创建实体类:创建一个实体类,用于映射Excel中的数据。 ```java public class User { private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 3. 导出Excel:使用EasyExcel提供的工具类进行导出操作。 ```java public void exportExcel(List<User> userList, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("用户列表", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), User.class).sheet("用户列表").doWrite(userList); } ``` 4. 导入Excel:使用EasyExcel提供的监听器类进行导入操作。 ```java public void importExcel(MultipartFile file) throws IOException { EasyExcel.read(file.getInputStream(), User.class, new AnalysisEventListener<User>() { @Override public void invoke(User user, AnalysisContext context) { // 处理每一行数据 } @Override public void doAfterAllAnalysed(AnalysisContext context) { // 所有数据处理完成后的操作 } }).sheet().doRead(); } ``` 以上就是使用Spring BootEasyExcel实现Excel导入导出的基本步骤。你可以根据自己的需求对代码进行适当的调整和扩展。希望对你有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值