Java 如何找到包含ApachePOI中图片的单元格
我尝试在xls文档中循环图像。我编写下一个代码:Java 如何找到包含ApachePOI中图片的单元格,java,image,excel,apache-poi,Java,Image,Excel,Apache Poi,我尝试在xls文档中循环图像。我编写下一个代码: HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); if (patriarch != null) { // Loop through the objects for (HSSFShape shape : patriarch.getChildren()) { if (shape instanceof HSSFPicture) { HSS
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
if (patriarch != null) {
// Loop through the objects
for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
if (picture.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_PICTURE){
if (picture.getImageDimension() != null) {
// how to get cell, which contains this picture
}
}
}
}
}
如何为工作表中的每张图片找到单元格?
更新,现在我编写下一个工作代码:
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
if (patriarch != null) {
// Loop through the objects
for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
if (picture.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_PICTURE){
if (picture.getImageDimension() != null) {
// how to get cell, which contains this picture
if (picture.getImageDimension() != null) {
Row row = sheet.getRow(picture.getPreferredSize().getRow1());
if (row != null) {
Cell cell = row.getCell(picture.getPreferredSize().getCol1());
HSSFPictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
File file = new File(PATH + picture.getFileName() + "." + pictureData.suggestFileExtension());
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
fop.write(data);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
如果像我一样,将来需要将图片与单元格关联,请尝试使用HashMap()
有人能为这个任务写一些例子吗?我想找到单元格,在excel的工作表中有一张图片。诀窍在于,什么图片不是单元格的值。试试picture.getPictureData();不,只有字节[]、格式、mimetype、文件扩展名等数据,但没有对任何单元格的引用。这可能有点晚,但如果您有解决方案,则应将其添加为答案,而不是编辑问题引用