首页 文章

Apache POI将图像插入excel文件,但它们未显示

提问于
浏览
1

我在linux上有一个gwt web应用程序 . 在服务器端,我试图通过Apache poi 3.10将一些图像插入excel文件 .

我从db获取了图像文件的输入流并将其发送到现有的excel文件 . 代码是:

Drawing patriarch = null;

 private void addImages(InputStream in, HSSFWorkbook requestReport, HSSFSheet sheet, int row, int  col) throws IOException {
    CreationHelper helper = requestReport.getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();

    byte[] bytes = IOUtils.toByteArray(in);
    int pictureIndex = requestReport.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_PNG);
  in.close();
    if (patriarch == null) {
        patriarch = sheet.createDrawingPatriarch();
    }
    anchor.setAnchorType(2);
    anchor.setRow1(row);
    anchor.setCol1(col);
    Picture picture = patriarch.createPicture(anchor, pictureIndex);

    picture.resize();
    picture.setLineStyle(HSSFPicture.LINESTYLE_DASHDOTGEL);
}

在这种情况下,我从DB获取Inputstream,我在picture.resize()上得到NoSuchElementException .

另一方面,如果我使用磁盘中的图像插入,则没有错误,图像也不会显示 .

in = new FileInputStream(“/ test.jpeg”)

任何帮助,将不胜感激 .

1 回答

  • 3

    未显示磁盘映像,因为您未在锚点中设置第二列和行 . 你应该添加:

    anchor.setRow2(row + 1);
    anchor.setCol2(col + 1);
    

    我不知道数据库输入流出错的原因是什么 . 该错误表明输入流为空(不包含数据),但无法从您的帖子中确定原因 .

相关问题