EasyPio导入导出excel表格

EasyPoi介绍:

EasyPoi是一个功能强大且易于使用的Java Excel操作框架,其主要特点如下:

  1. 简单易用:EasyPoi提供简洁而直观的API,使Java开发人员能够轻松地进行Excel导入导出操作,无需繁琐的代码和复杂的配置。

  2. 支持多种数据源:EasyPoi支持从数据库、List集合、Map等各种数据源快速生成Excel文件,并且可以将Excel文件中的数据导入到数据库或其他数据源中。

  3. 强大的导入导出功能:EasyPoi提供了丰富的导入导出功能,包括导出Excel文件、设置表头样式、数据格式化、合并单元格、设置列宽、设置公式等。同时,还支持导入Excel文件并自动映射到Java对象中,大大简化了数据导入的过程。

  4. 支持多种Excel格式:EasyPoi支持导入导出多种常见的Excel格式,包括xls、xlsx等,同时还支持导出csv、pdf等其他格式,满足不同场景下的需求。

  5. 高性能:EasyPoi通过优化底层算法和IO处理,提供了出色的性能表现,在海量数据的导入导出过程中能够保持较高的效率。

  6. 可扩展性强:EasyPoi支持用户自定义样式和格式,可以根据具体需求进行扩展和定制,满足各种复杂的导入导出场景。

  7. 运行稳定可靠:EasyPoi已在许多项目中得到广泛应用并验证了其稳定性和可靠性,可以放心使用。

使用:

导出excel

1.导包

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.2.0</version>
</dependency>

2.pojo加注解

pojo字段上的注解name属性对应excel表头

public class Student implements Serializable {    
    @Excel(name = "学生姓名")
    private String name;
    
    @Excel(name = "入学时间",exportFormat = "yyyy-MM-dd HH:mm")
    private Date createTime;
}

3.controller


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;

    @SneakyThrows //抛出异常,不建议使用
    @GetMapping
    public void exportData(HttpServletResponse response){
        //1.查询数据
        List<Student> datas = studentMapper.selectList();

        //2.封装成表格
        //参数1:表格标题,参数2:sheet名称
        ExportParams exportParams = new ExportParams("学生信息", "1班学生信息");
        //参数1:表格参数  参数2:实体类  参数3:数据
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, Student.class, datas);

        //3.返回表格
        //设置表格文件名字
        String fileName = "一班学生数据";
        fileName = URLEncoder.encode(fileName,"UTF8");
        //设置返回数据类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");

        //将表格输出
        sheets.write(response.getOutputStream());
    }
}

注意事项:前端的请求方式不能是Ajax异步请求,只能使用get方式

导入excel

1.导包(同上)

2.pojo加注解(同上)

3.controller

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    
    @SneakyThrows
    @PostMapping
    public void importData(MultipartFile file){

        //设置导入参数
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1); //标题占1行,默认0
        importParams.setHeadRows(1); //表头占1行,默认1

        //excel转POJO
        List<Student> studentList = ExcelImportUtil.importExcel(file.getInputStream(),
                Student.class, importParams);

        //添加到数据库
        Iterator<Student> iterator = studentList.iterator();
        while(iterator.hasNext()){
            Student studnet = iterator.next();
            studentMapper.insert(studnet);
        }
        System.out.println(studentList);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_58724261/article/details/131527257