简单例子随便记录下,后续完善
private static void exprotExcel() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // public static HSSFWorkbook listToExcelByModel(List list, String cols, String modelName){ // if(list == null || list.size() <= 0 // || cols == null || cols.equals("") // || modelName == null || modelName.equals("")){ // return null; // } // String[] col = stringToKenizer( cols, ","); String[] cols = new String[]{"nickname","userType","score"}; String[] col = new String[]{"姓名","年龄","性别"}; List<User> list = new ArrayList<User>(); User user = new User(); user.setNickname("测试"); user.setUserType((short)1); user.setScore(50); list.add(user); HSSFWorkbook wb = new HSSFWorkbook(); Sheet s = wb.createSheet("测试导出表格"); Row r = s.createRow(0); for(int i = 0; col != null && i < col.length; i++){ r.createCell(i).setCellValue(col[i]); } for(int i = 0; i < list.size(); i++){ Object temp = list.get(i); if(temp != null){ Row rr = s.createRow(i + 1); for(int j = 0; cols != null && j < cols.length; j++){ rr.createCell(j).setCellValue(BeanUtils.getProperty(temp, cols[j])); } } } try { FileOutputStream tt = new FileOutputStream(new File("/Users/yjpx/myspace/testFile/testExcel.xls")); wb.write(tt); tt.close(); // if(Util.checkNull(user.getDeptCode())){ // list = service.list(searchInfo,user.getDeptCode(), null); // String showCols = request.getParameter("showCols"); // String modelName = request.getParameter("modelName"); // HSSFWorkbook wb = Util.listToExcelByModel(list, showCols, modelName); // if(wb != null){ // String fileName="Àϸɲ¿ÐÅϢά»¤£¨¹²"+list.size()+"Ìõ£©"; // response.setContentType("application/msexcel"); // response.setHeader("Pragma", "no-cache"); // response.setHeader("Cache-Control", "no-cache"); // response.setHeader("Content-Disposition:", "attachment;filename=" + new String((fileName+".xls").getBytes("gb2312"), "iso8859-1")); // response.setHeader("Pragma", ""); // response.setHeader("Cache-Control", ""); // OutputStream servletOut = response.getOutputStream(); // try{ // // servletOut.write(baos.toByteArray()); // wb.write(servletOut); // }catch(Exception e){ // servletOut = null; // servletOut.close(); // e.printStackTrace(); // } // }else{ // renderText(response,"0"); // } // } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // return wb; }