poi 技术动态更新 Excel模板内容,动态更新内容

1.控制器方法


private URL base = this.getClass().getResource("");


/**
* 流拍之后,可以下载询价单 * * @param id 拍卖id * @param response */ @RequestMapping(value="/inquiryDownLoad",method = RequestMethod.GET) @ResponseBody public void inquiryDownLoad(String aucId,HttpServletResponse response) {
//日志记录
if (logger.isDebugEnabled()) { logger.debug("inquiryDownLoad, aucLotId ", aucId); }
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); AucLot aucLot = aucLotRepository.findOne(aucId); if(aucLot!=null){ SysUser sysUser=sysUserRepository.findOne(aucLot.crtUserId()); AucPrice aucPrice=aucPriceListService.findAucLotCurrentPrice(aucId);
try { //获取模板文件的目录地址 String fileDir = new File(base.getFile(), "../../../../../../doc/").getCanonicalPath(); //获取模板文件 File demoFile=new File(fileDir + "/inquiryPrice.xls"); //这里传入一个空模板 FileInputStream in = new FileInputStream(demoFile); //创建Excel的 JAVA 对象 HSSFWorkbook book = new HSSFWorkbook(in); HSSFSheet sheet = book.getSheetAt(0);//获取Excel的第一页内容 sheet.protectSheet("123456");//设置Excel编辑密码,没有密码不可以编辑 HSSFRow row = null; HSSFCell cell=null;

          //动态更新特定坐标位置的值
//公司留存 拍卖开始时间 row=sheet.getRow(6); cell=row.getCell(12); cell.setCellValue(sdf.format(aucLot.startTime())); //公司留存委托人姓名and手机号 row=sheet.getRow(9); cell=row.getCell(11); cell.setCellValue(aucLot.crtUserName()); row=sheet.getRow(11); cell=row.getCell(11); if(sysUser!=null && sysUser.mobile()!=null){ cell.setCellValue(sysUser.mobile().replace(sysUser.mobile().substring(3, 8), "****")); } //委托人留存 row=sheet.getRow(35); cell=row.getCell(11); cell.setCellValue(aucLot.crtUserName()); row=sheet.getRow(37); cell=row.getCell(11); if(sysUser!=null && sysUser.mobile()!=null){ cell.setCellValue(sysUser.mobile().replace(sysUser.mobile().substring(3, 8), "****")); } //拍品保留价and最高出价 row=sheet.getRow(21); cell=row.getCell(29); cell.setCellValue(aucLot.reservePrice().doubleValue()); row=sheet.getRow(22); cell=row.getCell(29); cell.setCellValue(aucPrice.price().doubleValue()); //设置下载题头 response.setContentType("application/xls"); String name = java.net.URLEncoder.encode(aucLot.goodsNo()+"测试.xls", "UTF8"); response.addHeader("Content-Disposition", "attachment; filename=" + name); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ServletOutputStream servletOS = response.getOutputStream(); book.write(ostream); servletOS.write(ostream.toByteArray()); servletOS.flush(); servletOS.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6407987.html