Apache POI 无法打开嵌入的文件

huangapple 未分类评论50阅读模式
英文:

Apache POI unable to open embedded files

问题

我正在尝试将一个PDF文件嵌入到Excel中,我成功地嵌入了文件,但它无法打开。Excel显示了我锚定的图像,但单击它时什么都没有发生,因为它只是一个图像。

  1. XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("dr.xlsx"));
  2. FileInputStream fs = new FileInputStream("test.pdf");
  3. FileInputStream fs1 = new FileInputStream("download.png");
  4. int pdf = wb.addOlePackage(IOUtils.toByteArray(fs), "text.pdf", "text.pdf", "text.pdf");
  5. int anc = wb.addPicture(IOUtils.toByteArray(fs1), HSSFPicture.PICTURE_TYPE_PNG);
  6. XSSFSheet sheet = wb.getSheetAt(0);
  7. XSSFDrawing draw = sheet.createDrawingPatriarch();
  8. ClientAnchor anchor = draw.createAnchor(0, 0, 0, 0, 1, 2, 2, 4);
  9. anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
  10. XSSFObjectData objectData = draw.createObjectData(anchor, pdf, anc);
  11. objectData.getCTShape().getNvSpPr().getCNvPr().setName("text.pdf");
  12. objectData.getCTShape().getNvSpPr().getCNvPr().setHidden(false);

Apache POI 无法打开嵌入的文件

英文:

I am trying to embed a PDF file in an excel, I am able to embed the file but it is not opening. Excel is showing the image I had anchored to, but on click of it, nothing happens as it is just an image.

  1. XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("dr.xlsx"));
  2. FileInputStream fs = new FileInputStream("test.pdf");
  3. FileInputStream fs1 = new FileInputStream("download.png");
  4. int pdf = wb.addOlePackage(IOUtils.toByteArray(fs), "text.pdf", "text.pdf", "text.pdf");
  5. int anc = wb.addPicture(IOUtils.toByteArray(fs1), HSSFPicture.PICTURE_TYPE_PNG);
  6. XSSFSheet sheet = wb.getSheetAt(0);
  7. XSSFDrawing draw = sheet.createDrawingPatriarch();
  8. ClientAnchor anchor = draw.createAnchor(0, 0, 0, 0, 1, 2, 2, 4);
  9. anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
  10. XSSFObjectData objectData = draw.createObjectData(anchor, pdf, anc);
  11. objectData.getCTShape().getNvSpPr().getCNvPr().setName( "text.pdf");
  12. objectData.getCTShape().getNvSpPr().getCNvPr().setHidden(false);

Apache POI 无法打开嵌入的文件

huangapple
  • 本文由 发表于 2020年5月30日 00:53:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/62090939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定