poi导出Excel

poi导出Excel

前端代码

	<button type="button" id="export">导出</button>
	
	<script type="text/javascript">
		$("#export").click(
	function(){
			window.location.href="/rest/food/exportExcel?ids="+ids;	
	}
	);
	</script>

后台代码

		
/**
 * 	导出Excel	
 * @param ids
 * @param response
 * @return
 * @throws IOException
 */
		  @RequestMapping(value ="/exportExcel", method = RequestMethod.GET)
		  @ResponseBody
		  public String exportExcel(@RequestParam Integer[] ids, HttpServletResponse response) throws IOException {
			  
			
		  //创建HSSFWorkbook对象(excel的文档对象)
		      HSSFWorkbook wb = new HSSFWorkbook();
		//建立新的sheet对象(excel的表单)
		HSSFSheet sheet=wb.createSheet("菜品表");
		
		//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
		HSSFRow row1=sheet.createRow(0);
		//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
		HSSFCell cell=row1.createCell(0);
		  //设置单元格内容
		cell.setCellValue("xxx商户菜品一览表");
		//合并单元格CellRangeAddress构造参数依次表示 起始行,截至行,起始列, 截至列
		sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
		//在sheet里创建第二行
		HSSFRow row2=sheet.createRow(1);    
		      //创建单元格并设置单元格内容
		      row2.createCell(0).setCellValue("菜品编号");
		      row2.createCell(1).setCellValue("菜品名称");    
		      row2.createCell(2).setCellValue("菜品分类");
		      row2.createCell(3).setCellValue("条形码");                         
		      
		      
		      //在sheet里创建第三行
		 	 HSSFRow row3=sheet.createRow(2);
		      row3.createCell(0).setCellValue("李明");
		      row3.createCell(1).setCellValue("As178");
		      row3.createCell(2).setCellValue(87);    
		      row3.createCell(3).setCellValue(78);   
		  //.....省略部分代码
		      ServletOutputStream out =null;
		      //设置导出的文件名字
		      String fileName="xxx商户菜品表";
		     response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"ISO-8859-1") + ".xls");
		      response.setContentType("application/msexcel;charset=GBK");  
		      out = response.getOutputStream();  
		      // excel生成完毕,写到输出流
		        try {
		            wb.write(out);
		            out.flush();
		            out.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        }
		      
		  return "success";
		    }
		
posted @ 2017-09-14 12:13  猥琐熊花子酱  阅读(311)  评论(0编辑  收藏  举报