Apache POI - 图像无法在Microsoft Excel中显示

时间:2018-03-29 10:35:47

标签: java image spring-boot apache-poi

我正在使用apache poi生成Excel工作表,我希望在左上角包含公司徽标。

我使用下面给出的代码从URL获取图像并将其添加到第一个单元格。正如我测试的那样,它不能在微软办公室工作,自由办公室但在Mac的数字工作

代码:

private void setOrganisationLogo(HSSFWorkbook workbook, HSSFSheet sheet) {
int pictureIdx = 0;
try {
  String orgUrl = "https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
  InputStream inputStream = new URL(orgUrl).openStream();
  byte[] bytes = IOUtils.toByteArray(inputStream);
  pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
  inputStream.close();
  CreationHelper helper = workbook.getCreationHelper();
  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setCol1(1);
  anchor.setRow1(1);
  Drawing drawing = sheet.createDrawingPatriarch();

  drawing.createPicture(anchor, pictureIdx);
} catch (IOException e) {
  e.printStackTrace();
}
}

任何帮助都将不胜感激。

0 个答案:

没有答案