【发布时间】:2009-07-14 13:44:52
【问题描述】:
如何在 poi 中不同的 HSSFCell 对象中添加图片?
我已经编写了一些添加图像的代码,但问题是,我添加了最后一张图像的单元格,该单元格只显示图像,而没有其他单元格显示图像...
感谢您的帮助...
我的代码是
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++;
}
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;
}
【问题讨论】:
标签: java apache excel spreadsheet