springMVC中导出excel案例

来源:互联网 发布:sql模糊查询索引 编辑:程序博客网 时间:2024/05/16 14:57

原博文地址:http://my.oschina.net/xiaoxiangdaizi/blog/491932?p=1#OSC_h3_4



  • 数据模型

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    /**
     * Created by lgq on 2015/8/13.
     */
    public class Student {
     
        private long id;
     
        private String name;
     
        private int age;
     
        private boolean sex;
     
        private Date birthday;
     
        public long getId() {
            return id;
        }
     
        public void setId(long id) {
            this.id = id;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            this.age = age;
        }
     
        public boolean isSex() {
            return sex;
        }
     
        public void setSex(boolean sex) {
            this.sex = sex;
        }
     
        public Date getBirthday() {
            return birthday;
        }
     
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }


    编写excel视图实现,继承springMVC中的AbstractExcelView类

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    /**
     * 学生excel导出类, 继承springMVC中的AbstractExcelView类,
     * 并实现buildExcelDocument()方法
     * Created by lgq on 2015/8/13.
     */
    public class StudentExcelView extends AbstractExcelView {
     
     
        @Override
        protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
     
            List<Student> studentList = (List<Student>) model.get("dataSet");
            HSSFSheet sheet = workbook.createSheet();
            sheet.setDefaultColumnWidth(12);
     
            HSSFCell cell = getCell(sheet,0,0);
            setText(cell, "ID");
            cell = getCell(sheet,0,1);
            setText(cell, "姓名");
            cell = getCell(sheet,0,2);
            setText(cell, "年龄");
            cell = getCell(sheet,0,3);
            setText(cell, "性别");
            cell = getCell(sheet,0,4);
            setText(cell, "生日");
     
            for (int i = 0; i < studentList.size(); i++) {
                HSSFRow row = sheet.createRow(i+1);
                Student student = studentList.get(i);
                // 处理列
                row.createCell(0).setCellValue(student.getId());
                row.createCell(1).setCellValue(student.getName());
                row.createCell(2).setCellValue(student.getAge());
                if(student.isSex()) {
                    row.createCell(3).setCellValue("男");
                else {
                    row.createCell(3).setCellValue("女");
                }
                row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday()));
            }
            String fileName = "学生信息.xls";
            // 处理中文文件名
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition""attachment;filename=" + fileName);
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
     
        }
    }


    编写controller类

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @Controller
    @RequestMapping(value = "/controller/student")
    public class StudentController {
     
     
        @RequestMapping(value = "/export")
        public ModelAndView export(ModelMap model) throws ParseException {
            List<Student> dataSet = new ArrayList<Student>();
            for (int i = 0; i < 20; i++) {
                Student student = new Student();
                student.setId(i);
                student.setName("lgq"+i);
                student.setAge(20);
                student.setSex(false);
                student.setBirthday(new Date());
                dataSet.add(student);
            }
            StudentExcelView studentExcelView = new StudentExcelView();
            model.put("dataSet", dataSet);
            return new ModelAndView(studentExcelView, model);
        }
     
    }


    导出数据展示


    目录[-]

  • 数据模型
  • 编写excel视图实现,继承springMVC中的AbstractExcelView类
  • 编写controller类
  • 导出数据展示
  • 数据模型

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    /**
     * Created by lgq on 2015/8/13.
     */
    public class Student {
     
        private long id;
     
        private String name;
     
        private int age;
     
        private boolean sex;
     
        private Date birthday;
     
        public long getId() {
            return id;
        }
     
        public void setId(long id) {
            this.id = id;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            this.age = age;
        }
     
        public boolean isSex() {
            return sex;
        }
     
        public void setSex(boolean sex) {
            this.sex = sex;
        }
     
        public Date getBirthday() {
            return birthday;
        }
     
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }


    编写excel视图实现,继承springMVC中的AbstractExcelView类

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    /**
     * 学生excel导出类, 继承springMVC中的AbstractExcelView类,
     * 并实现buildExcelDocument()方法
     * Created by lgq on 2015/8/13.
     */
    public class StudentExcelView extends AbstractExcelView {
     
     
        @Override
        protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
     
            List<Student> studentList = (List<Student>) model.get("dataSet");
            HSSFSheet sheet = workbook.createSheet();
            sheet.setDefaultColumnWidth(12);
     
            HSSFCell cell = getCell(sheet,0,0);
            setText(cell, "ID");
            cell = getCell(sheet,0,1);
            setText(cell, "姓名");
            cell = getCell(sheet,0,2);
            setText(cell, "年龄");
            cell = getCell(sheet,0,3);
            setText(cell, "性别");
            cell = getCell(sheet,0,4);
            setText(cell, "生日");
     
            for (int i = 0; i < studentList.size(); i++) {
                HSSFRow row = sheet.createRow(i+1);
                Student student = studentList.get(i);
                // 处理列
                row.createCell(0).setCellValue(student.getId());
                row.createCell(1).setCellValue(student.getName());
                row.createCell(2).setCellValue(student.getAge());
                if(student.isSex()) {
                    row.createCell(3).setCellValue("男");
                else {
                    row.createCell(3).setCellValue("女");
                }
                row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday()));
            }
            String fileName = "学生信息.xls";
            // 处理中文文件名
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition""attachment;filename=" + fileName);
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
     
        }
    }


    编写controller类

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @Controller
    @RequestMapping(value = "/controller/student")
    public class StudentController {
     
     
        @RequestMapping(value = "/export")
        public ModelAndView export(ModelMap model) throws ParseException {
            List<Student> dataSet = new ArrayList<Student>();
            for (int i = 0; i < 20; i++) {
                Student student = new Student();
                student.setId(i);
                student.setName("lgq"+i);
                student.setAge(20);
                student.setSex(false);
                student.setBirthday(new Date());
                dataSet.add(student);
            }
            StudentExcelView studentExcelView = new StudentExcelView();
            model.put("dataSet", dataSet);
            return new ModelAndView(studentExcelView, model);
        }
     
    }


    导出数据展示

    0 0