2009-07-14 223 views
7

如何在poi中的不同HSSFCell對象中添加圖像?如何在Apache 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; 
} 

回答

7

我希望你自己找到解決方案。如果沒有:
問題是你爲每個圖像創建一個新的partiarch。 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
您應該只創建一個族譜實例併爲其所有圖片使用其createPicture方法。

+0

我發現與解決方案相同...無論如何感謝您的回答... – Garudadwajan 2009-09-29 08:42:04

相關問題