Gar*_*jan 7 java apache excel spreadsheet
如何在poi中的不同HSSFCell对象中添加Image?
我写了一些添加图像的代码,但问题是,单元格是我添加的最后一个图像,该单元格只显示图像而不是其他单元格没有显示图像...
感谢您的帮助 ...
我的代码是
while(rs.next()){
HSSFCell cell = getHSSFCell(sheet, rowNo, cellNo);
cell.setCellValue(new HSSFRichTextString(rs.getString("TEST_STEP_DETAILS")) );
cell.setCellStyle(style);
String annotate = rs.getString("ANNOTATE");
if(annotate != null){
int index = getPicIndex(wb);
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(400,10,655,200,(short)cellNo,(rowNo+1),(short)cellNo,(rowNo+1));
anchor.setAnchorType(1);
patriarch.createPicture(anchor, index);
}
cellNo++;
}
Run Code Online (Sandbox Code Playgroud)
getPicIndex方法: -
public static int getPicIndex(HSSFWorkbook wb){
int index = -1;
try {
byte[] picData = null;
File pic = new File( "C:\\pdf\\logo.jpg" );
long length = pic.length( );
picData = new byte[ ( int ) length ];
FileInputStream picIn = new FileInputStream( pic );
picIn.read( picData );
index = wb.addPicture( picData, HSSFWorkbook.PICTURE_TYPE_JPEG );
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return index;
}
Run Code Online (Sandbox Code Playgroud)
我希望你自己找到解决方案.如果不是:
问题是你为每个图像创建一个新的partiarch.
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
您应该只创建一个patriarch实例并对所有图像使用其createPicture方法.