前端实现表格的导入和导出(后台接口)

在pom.xml文件中导入依赖

<!--        hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

在Controller层编写两个导入导出接口

需要注意的是导入时候应删除id一列,因为数据库是自增不需要

而且导出的没有别名,名称都和实体类的一致

下面是导出接口

/*
    导出接口
 */
@GetMapping("/export")
public void export(HttpServletResponse response)throws Exception{
    //查询出所有数据
    List<User> userList = userService.list();
    //在内存操作写出到浏览器
    ExcelWriter writer = ExcelUtil.getWriter(true);
    //一次性写出list对象到excel,使用默认样式,强制输出标题
    writer.write(userList,true);

    //设置浏览器响应格式
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    String fileName = URLEncoder.encode("用户信息", "UTF-8");
    response.setHeader("Conten-Disposition","attachment;filename="+fileName+".xlsx");

    ServletOutputStream out = response.getOutputStream();
    writer.flush(out,true);
    out.close();
    writer.close();
}
导入接口
​​​​​​​/*
    导入接口
 */
@PostMapping("/import")
public Boolean Exceimport(MultipartFile file)throws Exception{
    InputStream fileInputStream = file.getInputStream();
    ExcelReader reader = ExcelUtil.getReader(fileInputStream);
    List<User> users = reader.readAll(User.class);
    return userService.saveBatch(users);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷袭西兰花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值