springMVC中导出excel案例

数据模型

/**
 * 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类

/**
 * 学生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类

@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);
    }

}


导出数据展示

springMVC中导出excel案例_第1张图片


你可能感兴趣的:(springMVC,excel导出)