EasyExcel的使用

这篇博客主要介绍了在Java中如何使用EasyExcel库导出学生信息和实训安排到Excel文件,以及针对不同浏览器的响应编码问题进行处理。首先,通过查询条件获取数据,然后转换为Excel格式,并写入文件。其次,针对MSIE和Trident内核的浏览器,采用特定的编码方式来处理文件名,确保导出的文件名正确显示。
摘要由CSDN通过智能技术生成

1、导入依赖

String fileName = "D:\\test\\easyExcel\\"+ UUID.randomUUID()+".xlsx";
createNewFile(fileName);
QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status",1);
List<StudentInfo> studentInfos = studentInfoService.list(queryWrapper);
ArrayList<StudentExcelDate> list = new ArrayList<>();
for (StudentInfo info : studentInfos) {
    String classname ="null";
    String schoolName = "null";
    String groupName = "暂未分组";

    if (!StringUtils.isEmpty(info.getRelationClassId())){
            classname = classInfoService.getById(info.getRelationClassId()).getName();
    }
    if (!StringUtils.isEmpty(info.getRelationSchoolId())){
        schoolName = schoolService.getById(info.getRelationSchoolId()).getName();
    }
    if (!StringUtils.isEmpty(info.getGroupId())){
        groupName = groupInfoService.getById(info.getGroupId()).getName();
    }

    StudentExcelDate date = new StudentExcelDate();
    date.setId(info.getId());
    date.setGender(info.getGender().equals(1) ? "男" : "女");
    date.setName(info.getName());
    date.setIdcardNumber(info.getIdcardNumber());
    date.setStudentNum(info.getStudentNum());
    date.setTelephone(info.getTelephone());
    date.setGroup(groupName);
    date.setClassInfo(classname);
    date.setSchool(schoolName);
    list.add(date);
}

EasyExcel.write(fileName, StudentExcelDate.class).sheet("学生信息表").doWrite(list);
return CommonResult.success("ok",null);

2、导出列表

/**
     * @Description: 编码判断修改为UTF-8
     * @Author: LZH
     * @Date: 2021/10/29 10:23
     */
    private static final String ENCODE_1 ="MSIE";
    private static final String ENCODE_2 ="Trident";

    private static final String ENCODE_UTF ="UTF-8";

    private static final String CHAR_SET_NAME ="ISO-8859-1";

    /**
     * @Description: 导出列表(教师端、学生端)
     * @Author: LZH
     * @Date: 2021/10/25 14:06
     */
    @ApiOperation("导出列表(教师端、学生端)")
    @GetMapping("export")
    public CommonResult<Object> exportPracticeExcel(HttpServletResponse response,HttpServletRequest request,ManagerPageVo vo){

        try {
            //设置文件名
            String filenames = "实训安排表"+System.currentTimeMillis();
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains(ENCODE_1) || userAgent.contains(ENCODE_2)) {
                filenames = URLEncoder.encode(filenames,ENCODE_UTF);
            } else {
                filenames = new String(filenames.getBytes(ENCODE_UTF), CHAR_SET_NAME);
            }

            response.setContentType("application/json.ms-exce");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "filename=" + filenames + ".xlsx");

            QueryWrapper<Practice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);

            if (!StringUtils.isEmpty(vo)){
                if (!StringUtils.isEmpty(vo.getClassId())){
                    queryWrapper.eq("relation_class_id",vo.getClassId());
                }
            }

            List<Practice> list = practiceService.list(queryWrapper);

            List<PracticeExcelData> data = practiceService.exportFormat(list);

            EasyExcel.write(response.getOutputStream(),PracticeExcelData.class).sheet("实训安排").doWrite(data);

            return null;

        } catch (Exception e) {

            e.printStackTrace();
            return CommonResult.error(500,"导出异常");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值