最简单的excel文件的下载(未连接数据库)

public void info(){
// 创建一个map集合,这里设置单元格的数据,如果是从数据库拿则将数据库数据放入就行
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sequence", "数据");
		map.put("date", "数据");
		map.put("chetaihao", "数据");
		map.put("productName", "数据");
		map.put("specification", "数据");
		map.put("memo", "数据");
		map.put("inspectRecordBizList", "数据");
		map.put("chanpingmingcheng", "数据");
		map.put("shifouquanchengluyun", "数据");
		
 
		HSSFWorkbook wb = new HSSFWorkbook();//创建一个webbook,对应一个Excel文件 
		
		HSSFCellStyle textStyle = wb.createCellStyle();//用来设置单元格文本格式的属性参数
//		textStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 
		HSSFDataFormat format = wb.createDataFormat();
		textStyle.setDataFormat(format.getFormat("@"));
		
		Sheet sheet = wb.createSheet("模板名称");//在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		Row row = sheet.createRow(0);//创建单元格,并设置值表头 设置表头居中
		row.setRowStyle(textStyle);
		int i = 0;
//     将map集合循环放入excel对应的单元格
		for(String key : map.keySet()){
			Cell cell = row.createCell(i);
			cell.setCellStyle(textStyle);//设置单元格为文本格式
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue((String) map.get(key));
			i++;
		}
		Map<String, Object> maps = new HashMap<String, Object>();
		map.put("sequence", "第二行数据");
		map.put("date", "第二行数据");
		map.put("chetaihao", "第二行数据");
		map.put("productName", "第二行数据");
		map.put("specification", "第二行数据");
		map.put("memo", "第二行数据");
		map.put("inspectRecordBizList", "第二行数据");
		map.put("chanpingmingcheng", "第二行数据");
		map.put("shifouquanchengluyun", "第二行数据");
 
		Row rows = sheet.createRow(1);
		 i = 0;
//   循环map设置到对应的单元格,若要从数据库数据也同理
		for(String key : map.keySet()){
			Cell cell = rows.createCell(i);
			cell.setCellStyle(textStyle);//设置单元格为文本格式
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue((String) map.get(key));
			i++;
		}
		OutputStream fos = null;
		try {
			fos = getResponse().getOutputStream();
			String userAgent = getRequest().getHeader("USER-AGENT");
			String fileName = "一体机客户产品配置填报表规范模板";
			try {
				if(StringUtils.contains(userAgent, "Mozilla")){
					fileName = new String(fileName.getBytes(), "ISO8859-1");
				}else {
					fileName = URLEncoder.encode(fileName, "utf8");
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
 
			getResponse().setCharacterEncoding("UTF-8");
			getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");// 设置contentType为excel格式
			//响应给浏览器已下载的形式(让浏览器下载文件)
getResponse().setHeader("Content-Disposition", "Attachment;Filename="+ fileName+".xls");
			wb.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值