spring 下载 Excel(2)

搭建springMVC的环境 
添加Excel的操作包 poi-3.0.1.jar。 

控制器中的方法,这里用的是多动作控制器 

Java代码   收藏代码
  1. public ModelAndView downCardAccountExcel(HttpServletRequest request,  
  2.         HttpServletResponse response, Object command)  
  3.         throws ServletRequestBindingException {  
  4.   
  5.     ModelAndView mv = new ModelAndView();  
  6.     ExcelCardAccountView mvExcel = new ExcelCardAccountView();  
  7.       
  8.     String queryStr = ReqUtil.getUrl(request);  
  9.     Map<String,String> queryMap = ReqUtil.getParamMap(request);  
  10.       
  11.     Map map = new HashMap();  
  12.     if(queryMap.size()>1) {//查询  
  13.         List<Cardaccount> cardAccounts = cardAccountService.queryCardAccountBy(queryMap,0,maxDownloadCount);  
  14.         map.put("records", cardAccounts);  
  15.     }  
  16.       
  17.     return new ModelAndView(mvExcel,map);  
  18. }  


Excel视图类,并将相关的写数据操作放里面,注意要继承AbstractExcelView,让它为Excel视图的子类,并实现 buildExcelDocument方法即可。 
Java代码   收藏代码
  1. public class ExcelCardAccountView extends AbstractExcelView {  
  2.   
  3.     @Override  
  4.     protected void buildExcelDocument(Map map, HSSFWorkbook workbook,  
  5.             HttpServletRequest request, HttpServletResponse response) throws Exception {  
  6.   
  7.         int sheetRowNum = 0;    //行控制  
  8.           
  9.         //创建工作表  
  10.         HSSFSheet sheet = workbook.createSheet("卡账户查询");  
  11.           
  12.   
  13.         //取得模型数据  
  14.         List list = (List) map.get("records");  
  15.         if(list!=null && list.size()>0) {  
  16.             //创建标题  
  17.             HSSFRow titleRow = sheet.createRow((short) sheetRowNum++);  
  18.             HSSFCell titleCell = titleRow.createCell((short0);  
  19.             titleCell.setCellValue(new HSSFRichTextString("卡账户查询"));  
  20.   
  21.             //创建一个空行  
  22.             sheet.createRow(sheetRowNum++);  
  23.   
  24.             //创建数据表头  
  25.             String[] titles = { "账户号""卡号","会员号","商户号","分店号","余额","次数","积分","创建时间" ,"状态"};  
  26.             HSSFRow dataTitleRow = sheet.createRow((short) sheetRowNum++);  
  27.             for (int i = 0; i < titles.length; i++) {  
  28.                 HSSFCell cell = dataTitleRow.createCell((short) i);  
  29.                 cell.setCellValue(new HSSFRichTextString(titles[i]));  
  30.             }  
  31.               
  32.             String[] methodArray = { "getAccountId""getCardId","getMemberId","getMerchantsId","getBranchId","getBalance","getTimes","getIntegral","getGmtCreate" };  
  33.               
  34.               
  35.             //数据模型转换:创建表格数据  
  36.             Iterator<Cardaccount> iter = list.iterator();  
  37.             for (int i = sheetRowNum; i < list.size() + sheetRowNum; i++) {  
  38.                 if (iter.hasNext()) {  
  39.                     Cardaccount item = iter.next();  
  40.                     HSSFRow dataRow = sheet.createRow((short) (i));  
  41.                     ServUtil.writeRowData(item,dataRow,methodArray);    //写入行  
  42.                       
  43.                     //其他的数据转换  
  44.                     HSSFCell cell_0 = dataRow.createCell((short) methodArray.length);  
  45.                     if(item.getStatus()==1) {  
  46.                         cell_0.setCellValue(new HSSFRichTextString("正常"));  
  47.                     } else {  
  48.                         cell_0.setCellValue(new HSSFRichTextString("异常"));  
  49.                     }  
  50.                 }  
  51.             }         
  52.         } else {//没数据  
  53.             //创建提示  
  54.             HSSFRow titleRow = sheet.createRow((short) sheetRowNum++);  
  55.             HSSFCell titleCell = titleRow.createCell((short0);  
  56.             titleCell.setCellValue(new HSSFRichTextString("没有数据"));  
  57.         }  
  58.           
  59.     }  
  60.   
  61. }  


为了避免导出很多列时的重复工作,写了一个方法,将不用转换的数据列设置到行中,方法如下: 

Java代码   收藏代码
  1. /** 
  2.  * 将对象o按methodArray的顺序写入到dataRow的行中,在导出excel中用到 
  3.  * @param o             写入的对象 
  4.  * @param dataRow       写人的行 
  5.  * @param methodArray   拿取o对象的get方法列表 如 String[] paraArray = { "getAccountId", "getCardId"}; 
  6.  * 效果是,将o的accountId和cardId值设置到dataRow中 
  7.  */  
  8. public static void writeRowData(Object o, HSSFRow dataRow,String[] methodArray)  {  
  9.     Class<?> c = o.getClass();  
  10.       
  11.     for(int i=0;i<methodArray.length;i++) {  
  12.         HSSFCell cell = dataRow.createCell((short)i);   //创建一个行  
  13.         String methodName = methodArray[i];  
  14.         //根据paraArray中的顺序,依次取出o对应方法的值,设置到dataRow中  
  15.         Method m = null;  
  16.         try {  
  17.             m = c.getMethod(methodName);  
  18.         } catch (SecurityException e) {  
  19.             e.printStackTrace();  
  20.         } catch (NoSuchMethodException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         if(m!=null) {  
  24.             try {  
  25.                 String returnType = m.getReturnType().toString();   
  26.                 if(returnType.contains("String")) {  
  27.                     cell.setCellValue(new HSSFRichTextString((String)m.invoke(o)));  
  28.                 } else if(returnType.contains("Integer") || returnType.contains("int")) {  
  29.                     cell.setCellValue(SimpleFilter.filterNull((Integer)m.invoke(o)));  
  30.                 } else if(returnType.contains("Date")) {  
  31.                     cell.setCellValue(new HSSFRichTextString(SimpleDateUtils.getDateString((Date)m.invoke(o), null)));  
  32.                 }  
  33.             } catch (IllegalArgumentException e) {  
  34.                 e.printStackTrace();  
  35.             } catch (IllegalAccessException e) {  
  36.                 e.printStackTrace();  
  37.             } catch (InvocationTargetException e) {  
  38.                 e.printStackTrace();  
  39.             }  
  40.         } else {  
  41.             cell.setCellValue(new HSSFRichTextString(""));  
  42.         }  
  43.     }  
  44. }  

猜你喜欢

转载自yingpengfei1215.iteye.com/blog/1597810