poi导出Excel 不会产生垃圾文件的导出方式

@RequestMapping(value="/downEx",method=RequestMethod.GET)
	public void downEx(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap,IppResource ippResource,Integer ippTermId,boolean priceZero,String fileName,boolean ippCatalog)throws Exception{
		PageParameter page = super.initPage(request);
		Map<String, Object> map = new HashMap<String, Object>();
		page.setPageSize(100);//暂时定死
		//map.put("page", page);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		fileName = FileUtils.toUtf8String(fileName+df.format(new Date())+".xlsx");
		map.put("ippResource", ippResource);
		map.put("priceZero", priceZero);
		IppTerm ippTerm=ippResourceService.getIppTerm(ippTermId);
		map.put("ippTerm", ippTerm);
		//地市list
		List<IppRegionTermVO> ipreginList=ippResourceService.getRegionTermByTermList(ippTermId);
		modelMap.put("ipreginList", ipreginList);
		//材料list
		map.put("rtList", ipreginList);
		map.put("down", true);
		List<IppResource> resourceList = null;
		if(ippCatalog == true){//导出未分类材料
			resourceList=ippResourceService.getDownIppResourceNoCatalogPage(map);
		}else{//导出所有材料
			resourceList=ippResourceService.getDownIppResourcePage(map);
		}
		//信息价map
		Map<String,IppInfoPrice> priceMap = null;
		if(ippTermId!=null &&ippTermId!=0 && resourceList.size()!=0){
			priceMap=ippResourceService.getippInfoPriceMap(resourceList, ipreginList,ippTerm);
		}else{
			ippTermId=0;
		}
		//poi生成excel
		try {
			XSSFWorkbook wb = new XSSFWorkbook();
			treatExportData(wb,ipreginList, resourceList,priceMap,ippTermId);
			String disposition = "attachment;filename="+ fileName;
			response.setHeader("Content-disposition", disposition);  
			BufferedOutputStream fs=new BufferedOutputStream(response.getOutputStream());
			wb.write(fs); 
			fs.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public void treatExportData(XSSFWorkbook wb,List<IppRegionTermVO> ipreginList,List<IppResource> resourceList,Map<String,IppInfoPrice> priceMap,Integer ippTermId){
		if(resourceList.size()>0){
			int lSize = resourceList.size();
			int cSize = divide(lSize,Constants.EXCEL_EXPORT_SIZE);
			for(int j=0;j<cSize;j++){
				XSSFSheet sheet = wb.createSheet(); 
				XSSFRow row = sheet.createRow((int) 0);
				XSSFCell cell = row.createCell(0);
				cell.setCellValue("ID");  
				cell = row.createCell(1);  
				cell.setCellValue("编码");  
				cell = row.createCell(2);  
				cell.setCellValue("名称");  
				cell = row.createCell(3);  
				cell.setCellValue("规格");  
				cell = row.createCell(4);
				cell.setCellValue("单位");
				cell = row.createCell(5);
				cell.setCellValue("品牌");
				cell = row.createCell(6);
				cell.setCellValue("备注");
				if(ippTermId>0){
					for(int i=0;i<ipreginList.size();i++){
						IppRegionTermVO reg = ipreginList.get(i);
						cell =  row.createCell(i+7);
						cell.setCellValue("价格("+reg.getRegionName()+")");
					}
				}
				List<IppResource> subList = resourceList.subList(j*Constants.EXCEL_EXPORT_SIZE, ((j+1)*Constants.EXCEL_EXPORT_SIZE)>lSize?lSize:((j+1)*Constants.EXCEL_EXPORT_SIZE));
				for(int i=0;i<subList.size();i++){
					IppResource resource = resourceList.get(i);
					row = sheet.createRow(i+1);
					cell = row.createCell(0);
					cell.setCellValue(resource.getIppResourceId());  
					cell = row.createCell(1);  
					cell.setCellValue(resource.getCode());  
					cell = row.createCell(2);  
					cell.setCellValue(resource.getDescription());  
					cell = row.createCell(3);  
					cell.setCellValue(resource.getSpec());  
					cell = row.createCell(4);
					cell.setCellValue(resource.getUnit());
					cell = row.createCell(5);
					cell.setCellValue(resource.getBrand());
					cell = row.createCell(6);
					cell.setCellValue(resource.getRemark());
					if(ippTermId>0){
						for(int l=0;l<ipreginList.size();l++){
							IppRegionTermVO reg = ipreginList.get(l);
							if(priceMap.get(resource.getIppResourceId()+"_"+reg.getIppRegionTermId())!=null){
								cell =  row.createCell(l+7);
								cell.setCellValue(priceMap.get(resource.getIppResourceId()+"_"+reg.getIppRegionTermId()).getInfoPrice()==null?"":priceMap.get(resource.getIppResourceId()+"_"+reg.getIppRegionTermId()).getInfoPrice().toString());
							}
						}
					}
				}
			}
		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值