EXCEL POI导出简洁版

@RequestMapping(value="/exportExpertList")
    public String exportExpertList(@RequestParam(name = "name") String Name,
    							   @RequestParam(name = "researchDomain")String ResearchDomain,
    							   HttpServletResponse response) throws IOException {
    	  //处理前端参数为汉字时乱码问题
    	 String name=new String(Name.trim().getBytes("ISO-8859-1"), "UTF-8");
    	 String researchDomain=new String(ResearchDomain.getBytes("ISO-8859-1"), "UTF-8");
    	
		// 第一步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook book = new HSSFWorkbook();
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = book.createSheet("专家导出EXCEL文档");	
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setColumnWidth(2, 256*20); //设置某一列宽度
		sheet.setColumnWidth(3, 256*20); 
		sheet.setColumnWidth(4, 256*20);
		sheet.setColumnWidth(5, 256*20);
		sheet.setColumnWidth(6, 256*20);
		sheet.setColumnWidth(7, 256*20);
		
		HSSFCellStyle cellStyle2 = book.createCellStyle();
		HSSFDataFormat format = book.createDataFormat();
		cellStyle2.setDataFormat(format.getFormat("@"));// 设置单元格格式为文本
		
		HSSFCell cell = row.createCell(0);
		cell.setCellStyle(cellStyle2);
		cell.setCellValue(new HSSFRichTextString("姓名"));
		
		cell = row.createCell(1);
		cell.setCellStyle(cellStyle2);
		cell.setCellValue(new HSSFRichTextString("学历"));
		
		cell = row.createCell(2);
		cell.setCellStyle(cellStyle2);
		cell.setCellValue(new HSSFRichTextString("工作单位"));
		
		cell = row.createCell(3);
		cell.setCellStyle(cellStyle2);
		cell.setCellValue(new HSSFRichTextString("研究领域"));
		
		cell = row.createCell(4);
		cell.setCellStyle(cellStyle2);
		cell.setCellValue(new HSSFRichTextString("专家类别"));
		
		Map<String,String> parameters=new HashMap<>();
		parameters.put("name",name );
		parameters.put("researchDomain",researchDomain );
		
	    List<ExpertInfo> expertInfoList = new ArrayList<ExpertInfo>();
        expertInfoList = expertInfoService.queryExpertList(parameters);
		
		
	      for(int i=0;i<expertInfoList.size();i++){
	    	  ExpertInfo ExpertInfo = expertInfoList.get(i);
			  row = sheet.createRow((int) i + 1);
			  
			  int a=0;
			  String name1=(String) ExpertInfo.getName();
			  cell = row.createCell(a);
			  cell.setCellValue(new HSSFRichTextString(name1));
			  
			  String education=(String) ExpertInfo.getEducation();
			  cell = row.createCell(++a);
			  cell.setCellValue(new HSSFRichTextString(education));
			  
			  String talentcategory=(String) ExpertInfo.getCompany();
			  cell = row.createCell(++a);
			  cell.setCellValue(new HSSFRichTextString(talentcategory));
			  
			  String researchDomain1=(String) ExpertInfo.getResearchDomain();
			  cell = row.createCell(++a);
			  cell.setCellValue(new HSSFRichTextString(researchDomain1));
			  
			  String expertCategory=(String) ExpertInfo.getExpertCategory();
			  cell = row.createCell(++a);
			  cell.setCellValue(new HSSFRichTextString(expertCategory));
	      }

	      OutputStream os =  null;
			
	        // 取得输出流
	        try {
				os = response.getOutputStream();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
	        response.reset();// 清空输出
	
	   // 第五步 见文件存储到指定位置
	      String fileName = "专家导出EXCEL文档.xls";
	        //9.设置信息头
	       response.setContentType(fileName);
	        try {
				fileName = new String (fileName.getBytes ("utf-8"),"ISO8859-1");
			} catch (UnsupportedEncodingException e2) {
				e2.printStackTrace();
			}
	       response.setHeader("Content-Disposition","attachment;filename="+fileName);
	        //10.写出文件,关闭流
	        try {
	        	book.write(os);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
	        try {
	        	book.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		return null;
    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值