我正在尝试使用方法wbook.getAllpicture()读取xl_sheet中的图像;但我发现这些图像整体都在返回,我无法根据工作单位保持图像分离。
答案 0 :(得分:1)
在Excel
中,图片数据存储在Workbook
级别。这就是Workbook.getAllPictures实际得到的结果。
每张纸都有一个Drawing
层,悬浮在纸张上。在此Drawing
Shapes
中Workbook
锚定到工作表,也可能链接到Drawing
级别存储的图片数据。如果是这样,形状会显示该图片。
因此,为了使图片与图纸相关,我们必须在适当的import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
class ExcelGetXSSFPicturesWithPosition {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelWithImages.xlsx"));
for (Sheet sheet : workbook) {
String sheetname = sheet.getSheetName();
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing instanceof XSSFDrawing) {
for (XSSFShape shape : ((XSSFDrawing)drawing).getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture xssfPicture = (XSSFPicture)shape;
String shapename = xssfPicture.getShapeName();
int row = xssfPicture.getClientAnchor().getRow1();
int col = xssfPicture.getClientAnchor().getCol1();
System.out.println(
"Picture with Shapename: " + shapename +
" is located sheet: " + sheetname +
", row: " + row +
", col: " + col
);
}
}
}
}
workbook.close();
}
}
图层上运行,确定我们找到的图形是否与图片相关联。如果是这样,我们就会得到那张照片。
示例:
XSSFPicture
要从Route::get('{category}/{product}', function($category, $product)
{
$product = Product::where('product_title', strtolower($product))
->and('category', strtolower($category))
->get();
return $product;
});
获取图片数据,我们可以使用XSSFPicture.getPictureData。