列表导出(普通方法)

以学生表student(id,name,age,telephone,teacher_id,remark)与教师表teacher(id,name,class_name,telephone)为例

选择要导出的行数据,ids为选择的行id。

1.controller层 

     /**
     * 导出学生列表Excel
     */
    public void studentExcel(String ids) {
        //获取导出列表
        List<Student> list = studentService.getAllList(ids);
        //将导出列表生成excel
        studentService.toExcel(response, "", list);
    }

2.service层

     /**
     *  获取导出列
     */
    List<Student> getAllList(String ids); 
   
     /**
     *  导出excel
     */
    void toExcel(HttpServletResponse response, String path, List<Student> list);

    

3.serviceImp实现层

     /**
     *  获取导出列
     */
    @Override
    public List<Student> getAllList(String ids) {
        List<Student> list = studentDao.getAllList(StringUtils.stringToList(ids));
        list.forEach(a -> {

        });

        return list;
    }

    /**
     *  导出excel
     */
    @Override
    public void toExcel(HttpServletResponse response, String path, List<Student> list) {
        try {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String date = sd.format(new Date());
            String sheetName = "学生表" + "(" + date + ")";
            if (path != null && !"".equals(path)) {
                sheetName = sheetName + ".xls";
            } else {
                response.setHeader("Content-Type", "application/force-download");
                response.setHeader("Content-Type", "application/vnd.ms-excel");
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Expires", "0");
                response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre- 
                check=0");
                response.setHeader("Pragma", "public");
                response.setHeader("Content-disposition", "attachment;filename="
                        + new String(sheetName.getBytes("gbk"), "ISO8859-1") + ".xls");
            }

           
            for (Student s : list) {
                if (s.getAge<=20)) {
                    s.setRemark("20岁之下");
                } else {
                    s.setRemark("20岁之上");
                }
            }
               

            Map<String, String> mapFields = new LinkedHashMap<>();

            mapFields.put("name", "学生名称");
            mapFields.put("age", "年龄");
            mapFields.put("telephone", "联系电话");
            mapFields.put("remark", "备注");
          
            DeriveExcel.exportExcel(sheetName, list, mapFields, response, path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4.studentDao层

     /**
     *  获取导出列
     **/
    List<Student> getAllList(List<Long> ids);

5.xml层

<select id="getAllList" resultType="com.modules.student">
    select id,name,age,telephone,remark,teacher_id
    from student where 1=1
    <if test="list!=null and list.size()>0">
        and id in
        <foreach item="ids" index="index" collection="list"
                 open="(" separator="," close=")">
            #{ids}
        </foreach>
    </if>
</select>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值