数据库表数据导出为Excel

话不多说,火速需求实现。

      引入maven坐标依赖

        <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>easyexcel</artifactId>
               <version>2.1.1</version>
        </dependency>

        构建导出表字段所对应的实体对象。

public class AdminExcelProperty {
    @ExcelProperty(value = "用户id",index = 0)
    private Long id;
    @ExcelProperty(value = "用户名",index = 1)
    private String username;
    @ExcelProperty(value = "密码",index = 2)
    private String password;
    @ExcelProperty(value = "头像",index = 3)
    private String icon;
    @ExcelProperty(value = "邮箱",index = 4)
    private String email;

            对应mapper

public interface AdminMapper extends BaseMapper<AdminExcelProperty> {

}

        对应mapper.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.excel.mapper.AdminMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="org.example.excel.AdminExcelProperty">
        <id column="id" property="id" />
        <result column="username" property="username" />
        <result column="password" property="password" />
        <result column="icon" property="icon" />
        <result column="email" property="email" />
     </resultMap>

</mapper>

        对应service

public interface ExportService {
   void exportExcel(HttpServletResponse response);
}

        对应serviceimpl

@Service
public class ExportServiceImpl implements ExportService {
    //查询表中的数据
    @Autowired
    private  AdminMapper adminMapper;
    @Override
    public void exportExcel(HttpServletResponse response) {
        List<AdminExcelProperty> list = adminMapper.selectList(null);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 名字根据个人需求可以动态传入
            String fileName = URLEncoder.encode("ums_admin", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AdminExcelProperty.class).sheet("admin").doWrite(list);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }
}

     对应controller

@RestController
@RequestMapping(value =  "/export")
public class ExportDBController {
    @Autowired
    private ExportService exportService;

    @GetMapping(value = "/admin")
    public void exportAdminExcel(HttpServletRequest httpServletRequest, HttpServletResponse response){
        exportService.exportExcel(response);
    }
}

       直接调用接口实现,其中可以根据自己不同需求做相应的修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

那山川

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

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

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

打赏作者

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

抵扣说明:

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

余额充值