poi之Excel(在线生成)下载

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

poi之Excel下载

     @RequestMapping(value = "/xxx")
	public void importFileTemplate(HttpServletRequest req,HttpServletResponse response, RedirectAttributes redirectAttributes) {
		log.debug("下载");
		try{
			workbook = new HSSFWorkbook();// excel文件对象
			HSSFSheet sheet1 = workbook.createSheet("下载信息");// 工作表对象
			// 设置标题样式
			this.setHeadCellStyles(workbook, sheet1);
			// 设置列头样式
			this.setTitleCellStyles(workbook, sheet1);
			// 设置数据样式
			this.setDataCellStyles(workbook, sheet1);
			// 创建一个隐藏页、隐藏数据集和名称管理
			this.creatHideSheet(workbook);
			// 创建标题和列头数据
			String headName = "终端信息";
			this.creatAppRowHead(sheet1, headName);
			// 设置下拉框
			this.createSelectValidate(sheet1);
			
			/**************************** 输出流 *****************************************/
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String filename = POIUtils.encodeFileName(req, headName);
			OutputStream os = response.getOutputStream();// 取得输出流
			response.setHeader("Content-disposition", "attachment;filename="+ filename + ".xls");
			workbook.write(os);
			os.close();

			System.out.println("导出成功!");
//			return null;
		}catch(Exception e){
			addMessage(redirectAttributes, "导入模板下载失败!失败信息:" + e.getMessage());
		}
//		return "redirect:/xxx";
	}

  

详细设置请继续阅读下一贴。

http://www.cnblogs.com/guoziyi/p/7121169.html

原文地址:https://www.cnblogs.com/guoziyi/p/7121030.html