@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());
}
}
}
}
}
}
}
poi导出Excel 不会产生垃圾文件的导出方式
最新推荐文章于 2024-06-06 18:48:43 发布