springMVC实现excel导出(简单封装版)

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
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 getSex() {

        return sex;

    }

 

    public void setSex(boolean sex) {

        this.sex = sex;

    }

 

    public Date getBirthday() {

        return birthday;

    }

 

    public void setBirthday(Date birthday) {

        this.birthday = birthday;

    }

}

编写默认的ExcelView

public class DefaultExcelView extends AbstractExcelView {

 

 

    @Override

    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {

 

        List<Object> dataSet = (List<Object>) model.get("dataSet");

        String[] keys = (String[])model.get("keys");

        String[] titles = (String[])model.get("titles");

        String fileName = (String)model.get("fileName");

        HSSFSheet sheet = workbook.createSheet();

        sheet.setDefaultColumnWidth(12);

 

        for (int i = 0; i < titles.length; i++) {

            HSSFCell cell = getCell(sheet,0,i);

            setText(cell, titles[i]);

        }

 

        for (int i = 0; i < dataSet.size(); i++) {

            HSSFRow row = sheet.createRow(i+1);

            Object obj = dataSet.get(i);

            // 处理列

            for(int j=0; j < keys.length; j++) {

                row.createCell(j).setCellValue(getFieldValueByName(keys[j], obj));

            }

        }

 

        fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1") + ".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();

 

    }

 

    /**

     * 属性中的方法都是getXXX和setXXX,不存在isXXX

     * @param fieldName

     * @param obj

     * @return

     */

    protected String getFieldValueByName(String fieldName, Object obj) {

        String firstLetter = fieldName.substring(0,1).toUpperCase();

        String getter = "get" + firstLetter + fieldName.substring(1);

        try {

            Method method = obj.getClass().getMethod(getter, new Class[]{});

            Object value = method.invoke(obj, new Object[]{});

            return value.toString();

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("属性不存在!");

        }

        return "";

    }

}

在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);

        }

        String[] keys = new String[]{"id", "name", "age", "sex","birthday"};

        String[] titles = new String[]{"编号", "姓名", "年龄", "性别", "生日"};

        String fileName = "学生信息";

        DefaultExcelView defaultExcelView = new DefaultExcelView();

        model.put("dataSet", dataSet);

        model.put("keys", keys);

        model.put("titles", titles);

        model.put("fileName", fileName);

        return new ModelAndView(defaultExcelView, model);

    }

 

}

标签: isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java Velocity 的宏定义实例

下一篇:Java 使用 Collections.frequency 找出重复的单词