Spring MVC导入excel时分页

/**
  * 导出列表为excel
  * @param request
  * @param response
  * @return
  * @throws BusinessException
  */

 public ModelAndView exportExcel(HttpServletRequest request, HttpServletResponse response) throws BusinessException{
 
 
  //查询记录集
  List  resultVO  = null;
  try{
   resultVO = null;//得到一个list
  }catch (Exception e) {
   hashMap.put("errorMessage", e);
   return new ModelAndView(failView, hashMap);
  }
 
        //总共要导出的记录数
        int totleCount = resultVO.getTotalRowCount();
        if(totleCount==0){
         hashMap.put("errorMessage", new BusinessException("p.jbeduhai.0")); //属性文件定义code
   return new ModelAndView(failView, hashMap);
        }
       
        String fileName = "xxxxxx";
        response.reset();//清空输出流
        response.setHeader("content-disposition","attachment;filename="+ fileName +".xls");
        response.setContentType("application/msexcel");//定义输出类型
       
        OutputStream os = null;
        WritableWorkbook book = null;
        try{
         os = response.getOutputStream();//取得输出流
         book = Workbook.createWorkbook(os);
        }catch (Exception e) {
   throw new SystemException("建立excel文件输出流时发生异常",e);
  }
       
        //第几个工作区
        int sheetNum = 1;
        NumberFormat nf = new jxl.write.NumberFormat("#0.00");
        WritableCellFormat wcfN = new WritableCellFormat(nf);
        Number labelNF;
        QueryVO cell; //显示 vo类定义
       
        WritableSheet sheet = null; //设置工作区
        int row = 0;//行
       
        List tcList = null;//得到一个list
        for(int i=0; i <= totleCount/pageSize;i++){
         try{
          resultVO  =null
         }catch (Exception e) {
       hashMap.put("errorMessage", e);
       return new ModelAndView(failView, hashMap);
      }
         tcList = resultVO.getDataList();
         for (QueryVO qcVO : tcList) {
          //判断是否超过当前工作区显示的的最大条数
          if(row==0 || row > maxRecord){
           //判断是否要创建新的工作区
           try {
      sheet = getSheet(book, sheetNum);
     } catch (Exception e) {
      throw new SystemException("新建excel工作区时发生异常",e);
     }
           row = 1;
           sheetNum++;
          }
          cell = qcVO;
          try {
           sheet.addCell(new Label(0,row,cell.getTbId().toString()));
              sheet.addCell(new Label(1,row,"aaa");
              labelNF = new Number(2,row,cell.getxx().doubleValue(), wcfN);
              sheet.addCell(labelNF);//输出数据为number型
             
              labelNF = new Number(3,row,cell.getxx().doubleValue(), wcfN);
              sheet.addCell(labelNF);//输出数据为number型
             
              sheet.addCell(new Label(4,row,"ccc");
             
          }catch (Exception e) {
           throw new SystemException("写excel文件记录时发生异常",e);
    }
          row ++;
         }
         pageNum++;
        }
 
  try {
   book.write();
   book.close();
   os.close();
  } catch (IOException e) {
   throw new SystemException("关闭文件输出流时发生异常",e);
  }
  return null;
 }

你可能感兴趣的:(spring,mvc,工作,Excel,OS)