Jxls导出excel的若干方式总结(十二)-- 在导出报表中动态插入图片
模板
List supplyAreaList = saBiz.getSupplyAreaById(supplyAreaId);
SupplyArea sa = (SupplyArea) supplyAreaList.get(0);
Long id = sa.getSupplyAreaId();
List qcList = new ArrayList();
QueryCondition idObj = new QueryCondition();
idObj.setFieldName("supplyAreaId");
idObj.setQueryOperator(QueryOperator.le);
idObj.setValue(id);
qcList.add(idObj);
List recordsList = saBiz.getRecords(qcList, new SupplyArea());
String templateDir = "D:/excel/template/SupplyAreaDynaInertPicture.xls";
String targetDir="D:/excel/export/testDynaInertPicture.xls";
InputStream is = new FileInputStream(templateDir);
Map beans = new HashMap();
beans.put("suplyArea", recordsList);
//关联模板
XLSTransformer transformer = new XLSTransformer();
HSSFWorkbook workBook = transformer.transformXLS(is, beans);
HSSFSheet sheet = workBook.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//创建绘图工具对象放循环外可正确显示
for(int i=0;i<recordsList.size();i++){
System.out.println("test1---->"+i);
//将图片以字节流的方式输入输出
String picture = "D:/excel/picture/picture1.jpg";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedImage BufferImg = ImageIO.read(new File(picture));
ImageIO.write(BufferImg, "JPEG", bos);
//public HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2,int row2)
HSSFClientAnchor anchor = new HSSFClientAnchor(5,0,405,250,(short) 6, i*7,(short)7,i*7+2);
//HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//创建绘图工具对象放循环内可显示不正patriarch.createPicture(anchor,workBook.addPicture(bos.toByteArray(),
workBook.PICTURE_TYPE_JPEG));
}
OutputStream os = new FileOutputStream(targetDir);
workBook.write(os);
is.close();
os.flush();
os.close();
导出结果