List<InspectionEnclosure> enclosureList = enclosureDao.getEnclosureByFatherId(bridgeSpace.getId()); int tofirstMer = 0; if(enclosureList!=null&&enclosureList.size()>0){ int a = 0; for(int j =0;j<enclosureList.size();j++){//循环向Excel模板中插入图片 if(enclosureList.get(j).getDocumentURL()!=null&&!"".equals(enclosureList.get(j).getDocumentURL())){ try { BufferedImage bufferImg = null; ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File(URLDecoder.decode(request.getSession().getServletContext().getRealPath(enclosureList.get(j).getDocumentURL().split("ACInspection")[1]),"utf-8"))); ImageIO.write(bufferImg, "png", byteArrayOut); Drawing patriarch = sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,(short) 0, (25+a), (short) 7, (30+a)); patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), workbook.PICTURE_TYPE_PNG)); } catch (IOException e) { e.printStackTrace(); } a+=6; }
//由于模板大小固定,图片过多会将模板覆盖,需动态增加模板行数 for(int x=0;x<6;x++){ sheet.shiftRows(25, sheet.getLastRowNum(), 1,true,false); sheet.createRow(25); tofirstMer++; } } }
//合并插入的行
CellRangeAddress region4 = new CellRangeAddress(50,50+tofirstMer,(short)0,(short)7); sheet.addMergedRegion(region4);