栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

一、引入依赖
 
        
            com.alibaba
            easyexcel
            ${easyexcel.version}
        

文档地址: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 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 list = new ArrayList<>(1);
        List errMsgList = new ArrayList<>(1);
        ExcelListener excelListener = new ExcelListener();
        Object Object1 = ExcelUtil.readExcel(file,ProjectExpView.class,0,excelListener);
        list = (List) Object1;
        projectService.importProject(list);
        return ResponseResult.importSuccess();
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号