列表導出(普通方法)

以學生表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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章