apache poi:电子表格中的图像与工作簿相关联,无法检索/链接到单个工作表。

时间:2018-03-10 06:47:28

标签: java excel apache-poi

我正在尝试使用方法wbook.getAllpicture()读取xl_sheet中的图像;但我发现这些图像整体都在返回,我无法根据工作单位保持图像分离。

1 个答案:

答案 0 :(得分:1)

Excel中,图片数据存储在Workbook级别。这就是Workbook.getAllPictures实际得到的结果。

每张纸都有一个Drawing层,悬浮在纸张上。在此Drawing ShapesWorkbook锚定到工作表,也可能链接到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