我在 Linux 上有一个 gwt Web 应用程序。在服务器端,我尝试通过 Apache poi 3.10 将一些图像插入到 Excel 文件中。
我从数据库获取图像文件的输入流并将其发送到现有的 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);
}
在这种情况下,我从数据库获取输入流,我在 picture.resize() 上得到 NoSuchElementException。
另一方面,如果我使用磁盘中的图像插入,则不会出现错误并且不会显示图像。
in = new FileInputStream("/test.jpeg")
如有任何帮助,我们将不胜感激。
最佳答案
磁盘镜像未显示,因为您未在 anchor 中设置第二列和行。您应该添加:
anchor.setRow2(row + 1);
anchor.setCol2(col + 1);
我不知道数据库的输入流出现错误的原因是什么。该错误表明输入流为空(不包含数据),但无法从您的帖子中确定原因。
关于java - Apache POI将图像插入到excel文件中,但不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471770/