标签:
/** * 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 getSex() { 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类, * 并实现buildExcelDocument()方法 * Created by lgq on 2015/8/13. */ 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 @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); } }
数据model中只能有getXXX方法,不能有isXXX方法。
不能处理对象特定类型的值,比如上图中boolean和Date类型。
IE浏览器下存在中文文件名称乱码的问题。
标签:
原文地址:http://my.oschina.net/xiaoxiangdaizi/blog/491976