【问题标题】:how to add images in HSSFCell in apache POI?如何在 Apache POI 的 HSSFCell 中添加图像?
【发布时间】: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


    【解决方案1】:

    我希望您自己找到了解决方案。如果没有:
    问题是您为每个图像创建了一个新的partiarch。 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    您应该只创建一个 patriarch 实例并将其 createPicture 方法用于所有图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      相关资源
      最近更新 更多