【问题标题】:images are inserted to excel file by Apache POI ,but they are not shown图像由 Apache POI 插入到 excel 文件中,但未显示
【发布时间】:2013-12-26 15:07:15
【问题描述】:

我在 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")

任何帮助将不胜感激。

【问题讨论】:

  • 我在从远程位置访问输入流时遇到了同样的问题。我的输入流不是空的,因为我在调试时可以看到原始数据。您是如何解决数据库流式传输问题的?谢谢

标签: java excel apache-poi


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    相关资源
    最近更新 更多