Apache POI 无法打开嵌入的文件

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

Apache POI unable to open embedded files

问题

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

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("dr.xlsx"));
FileInputStream fs = new FileInputStream("test.pdf");
FileInputStream fs1 = new FileInputStream("download.png");
int pdf = wb.addOlePackage(IOUtils.toByteArray(fs), "text.pdf", "text.pdf", "text.pdf");
int anc = wb.addPicture(IOUtils.toByteArray(fs1), HSSFPicture.PICTURE_TYPE_PNG);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFDrawing draw = sheet.createDrawingPatriarch();
ClientAnchor anchor = draw.createAnchor(0, 0, 0, 0, 1, 2, 2, 4);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
XSSFObjectData objectData = draw.createObjectData(anchor, pdf, anc);
objectData.getCTShape().getNvSpPr().getCNvPr().setName("text.pdf");
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.

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("dr.xlsx"));
FileInputStream fs = new FileInputStream("test.pdf");
FileInputStream fs1 = new FileInputStream("download.png");
int pdf = wb.addOlePackage(IOUtils.toByteArray(fs), "text.pdf", "text.pdf", "text.pdf");
int anc = wb.addPicture(IOUtils.toByteArray(fs1), HSSFPicture.PICTURE_TYPE_PNG);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFDrawing draw = sheet.createDrawingPatriarch();
ClientAnchor anchor = draw.createAnchor(0, 0, 0, 0, 1, 2, 2, 4);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
XSSFObjectData objectData = draw.createObjectData(anchor, pdf, anc);
objectData.getCTShape().getNvSpPr().getCNvPr().setName( "text.pdf");
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:

确定