javaWeb导出POI 操作excel文件

public ModelAndView forExportData(HttpServletRequest request,
   HttpServletResponse response) throws IOException{
   String ids = request.getParameter("ids");
   ExportExcel exportExcel =  new ExportExcel();
   exportExcel.createTitle();
   String fileNameString = exportExcel.toUtf8String("test");
   String searchName = request.getParameter("searchName");
   String nameString =  java.net.URLDecoder.decode(searchName,"UTF-8");
   String searchUnit = request.getParameter("searchUnit");
   String searchSubmittime = request.getParameter("searchSubmittime");
   String xlsPath =  request.getSession().getServletContext().getRealPath("/");
   try {
    FileInputStream filein =  new FileInputStream(xlsPath+"jyhptXls.xls");
    HSSFWorkbook workbookh = new HSSFWorkbook(filein);
    HSSFSheet sheet = workbookh.getSheetAt(0);//获取第一个工作表
    HSSFCellStyle style =  workbookh.createCellStyle();
    List list = proProposalsService.getProProposalsByList(ids,nameString,searchUnit,searchSubmittime);
    int i = 1;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
     i++;
     DataObject dataObject = (DataObject) iterator.next();
     exportExcel.createBody(dataObject.getFieldValue("NAME"), dataObject.getFieldValue("UNITNAME"), dataObject.getFieldValue("TYPE"), dataObject.getFieldValue("PERSONNAME"), dataObject.getFieldValue("CONTACTNAME"), dataObject.getFieldValue("TELEPHONE"), dataObject.getFieldValue("SUBMITTIME"),
       dataObject.getFieldValue("PRO_BASIS"), dataObject.getFieldValue("BUS_IMPACT"), dataObject.getFieldValue("BETTER_CHANCE"), dataObject.getFieldValue("CHANCE_BUS_RANGE"),i, sheet ,style);
    }
        response.setContentType("application/vnd.ms-excel"); 
           response.setHeader("Content-disposition", "attachment;filename="+fileNameString+".xls"); 
           OutputStream ouputStream = response.getOutputStream(); 
           workbookh.write(ouputStream);
           ouputStream.flush(); 
           ouputStream.close();
           filein.close();
           System.out.println(i);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return null;
  
  
 }

 

 

 

jsp页面用form提交

var uri = “路劲” 

 location.href=uri;

你可能感兴趣的:(javaWeb导出POI 操作excel文件)