java导出excel模板_poi 实战代码---导出Excel(根据模板导出)

/*** 导出excel

*@paramrequest

*@paramresponse

*@return*@throwsException*/@RequestMapping("exportExcel")

@Action(description="导出excel")public void exportExcel(HttpServletRequest request, HttpServletResponse response) throwsException

{//获取实体类,并取得时刻点的值,存放到map里面

String id = RequestUtil.getString(request,"id");

LoadDate ld= newLoadDate();

ld=loadDateService.getById(Long.parseLong(id));

String mapVal=ld.getValue();

JSONObject json=JSONObject.fromObject(mapVal);

Map map = new HashMap();if(BeanUtils.isNotEmpty(json)) {

Iterator iterator =json.keys();while(iterator.hasNext()) {

String ie=(String) iterator.next();

map.put(ie, json.getString(ie));

}

}

String dirPath= FileUtil.getRootPath() + File.separator+"commons" +File.separator+"template"+File.separator+"exportMode"+File.separator;

String fileName="loadTemplate.xls";

FileInputStream inStream= new FileInputStream(new File(dirPath+fileName));//读取excel模板

HSSFWorkbook wb = new HSSFWorkbook(inStream); //读取excel模板//读取了模板内所有sheet内容

HSSFSheet sheet = wb.getSheetAt(0);

HSSFCell cell= null;//在相应的单元格进行赋值

for(int i=2;i<=97;i++){

cell= sheet.getRow(i).getCell(1);

String tmp=cell.getStringCellValue();

cell.setCellValue(map.get(tmp));

}for(int i=98;i<=102;i++){

cell= sheet.getRow(i).getCell(1);

String tmp=cell.getStringCellValue();if("ycgfdl".equals(tmp)){if(BeanUtils.isNotEmpty(ld.getYcgfdl())){

cell.setCellValue(ld.getYcgfdl());

}else{

cell.setCellValue("");

}

}else if("ycqdl".equals(tmp)){if(BeanUtils.isNotEmpty(ld.getYcqdl())){

cell.setCellValue(ld.getYcqdl());

}else{

cell.setCellValue("");

}

}else if("yccfdLi".equals(tmp)){if(BeanUtils.isNotEmpty(ld.getYccfdLi())){

cell.setCellValue(ld.getYccfdLi());

}else{

cell.setCellValue("");

}

}else if("yccfdLl".equals(tmp)){if(BeanUtils.isNotEmpty(ld.getYccfdLl())){

cell.setCellValue(ld.getYccfdLl());

}else{

cell.setCellValue("");

}

}else if("ycdgdl".equals(tmp)){if(BeanUtils.isNotEmpty(ld.getYcdgdl())){

cell.setCellValue(ld.getYcdgdl());

}else{

cell.setCellValue("");

}

}

}

response.setContentType("application/octet-stream;charset=UTF-8");

response.setHeader("Content-Type","application/vnd.ms-excel");

response.setHeader("Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("GB2312"), "8859_1"));

response.addHeader("Pargam", "no-cache");

response.addHeader("Cache-Control", "no-cache");

OutputStream out=response.getOutputStream();

wb.write(out);

out.flush();

out.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值