如何使用Apache POI检查单元格是否包含图像?(How do I check if a cell contains an image using Apache POI?)

编程入门 行业动态 更新时间:2024-10-19 17:29:15
如何使用Apache POI检查单元格是否包含图像?(How do I check if a cell contains an image using Apache POI?)

我有以下代码块:

File file = new File("myFile.xlsx"); // my file inputStream = new FileInputStream(file); System.out.println("reading"); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet sh = wb.getSheetAt(0); // first sheet Iterator rowIter = sh.rowIterator(); while(rowIter.hasNext()){ // iterate over all rows System.out.println("New Row "); // notify of new row Row myRow = (Row) rowIter.next(); Iterator cellIter = myRow.cellIterator(); while(cellIter.hasNext()){ // iterate over all cells in row XSSFCell myCell = (XSSFCell) cellIter.next(); //how can I check that myCell has an image? //(I'm expecting it to be in the fourth cell each time) System.out.print(" " + myCell); // output cell content on same line } } inputStream.close();

我正在使用Apache POI来读取.xlsx文件。 每行可以包含具有由用户粘贴到其中的图像的单元格。

我希望抓住图像并将其编码为base64。 在迭代电子表格时如何检查单元格是否包含图像?

我已经阅读过http://poi.apache.org/spreadsheet/quick-guide.html#Images ,但这涉及首先获取所有图像,然后迭代它们。 我想迭代细胞并检查图像。

I have the following block of code:

File file = new File("myFile.xlsx"); // my file inputStream = new FileInputStream(file); System.out.println("reading"); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet sh = wb.getSheetAt(0); // first sheet Iterator rowIter = sh.rowIterator(); while(rowIter.hasNext()){ // iterate over all rows System.out.println("New Row "); // notify of new row Row myRow = (Row) rowIter.next(); Iterator cellIter = myRow.cellIterator(); while(cellIter.hasNext()){ // iterate over all cells in row XSSFCell myCell = (XSSFCell) cellIter.next(); //how can I check that myCell has an image? //(I'm expecting it to be in the fourth cell each time) System.out.print(" " + myCell); // output cell content on same line } } inputStream.close();

I am using Apache POI to read an .xlsx file. Each row can contain a cell with an image which was pasted into it by a user.

I'm hoping to grab the image and encode it to base64. How do I check if a cell contains an image while iterating through the spreadsheet?

I've already read http://poi.apache.org/spreadsheet/quick-guide.html#Images , but that deals with getting all images first, then iterating through them. I'd like to iterate through cells and check for images.

最满意答案

以下代码遍历第一张表的所有图像。 因此,您不需要遍历行/列,而是直接获取位置。 除了OneCellAnchor和TwoCellAnchor之外,还有AbsoluteAnchors ,它们不能直接与细胞相关联。

import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.*; import org.apache.poi.xssf.usermodel.*; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*; public class Images2Cells { public static void main(String[] args) throws Exception { OPCPackage opc = OPCPackage.open("auto.xlsx", PackageAccess.READ); XSSFWorkbook book = new XSSFWorkbook(opc); XSSFSheet sheet = book.getSheetAt(0); for (POIXMLDocumentPart pdp : sheet.getRelations()) { if (!XSSFRelation.DRAWINGS.getRelation().equals(pdp.getPackageRelationship().getRelationshipType())) continue; PackagePart drawPP = pdp.getPackagePart(); WsDrDocument draw = WsDrDocument.Factory.parse(drawPP.getInputStream()); for (CTOneCellAnchor oneAnc : draw.getWsDr().getOneCellAnchorList()) { String picId = oneAnc.getPic().getBlipFill().getBlip().getEmbed(); PackageRelationship pr = drawPP.getRelationship(picId); PackagePart imgPP = drawPP.getRelatedPart(pr); // byte imgBytes[] = IOUtils.toByteArray(imgPP.getInputStream()); System.out.println(imgPP.getPartName() +" - Col: "+oneAnc.getFrom().getCol() +" - Row: "+oneAnc.getFrom().getRow() ); } for (CTTwoCellAnchor twoAnc : draw.getWsDr().getTwoCellAnchorList()) { String picId = twoAnc.getPic().getBlipFill().getBlip().getEmbed(); PackageRelationship pr = drawPP.getRelationship(picId); PackagePart imgPP = drawPP.getRelatedPart(pr); System.out.println(imgPP.getPartName() +" - Col1: "+twoAnc.getFrom().getCol() +" - Row1: "+twoAnc.getFrom().getRow() +" - Col2: "+twoAnc.getTo().getCol() +" - Row2: "+twoAnc.getTo().getRow() ); } } opc.revert(); } }

The following code iterates through all images of the first sheet. So you don't need to iterate through rows/cols, but get the positions straight away. Apart of OneCellAnchor and TwoCellAnchor there are also AbsoluteAnchors which can't be directly associate with a cell.

import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.*; import org.apache.poi.xssf.usermodel.*; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*; public class Images2Cells { public static void main(String[] args) throws Exception { OPCPackage opc = OPCPackage.open("auto.xlsx", PackageAccess.READ); XSSFWorkbook book = new XSSFWorkbook(opc); XSSFSheet sheet = book.getSheetAt(0); for (POIXMLDocumentPart pdp : sheet.getRelations()) { if (!XSSFRelation.DRAWINGS.getRelation().equals(pdp.getPackageRelationship().getRelationshipType())) continue; PackagePart drawPP = pdp.getPackagePart(); WsDrDocument draw = WsDrDocument.Factory.parse(drawPP.getInputStream()); for (CTOneCellAnchor oneAnc : draw.getWsDr().getOneCellAnchorList()) { String picId = oneAnc.getPic().getBlipFill().getBlip().getEmbed(); PackageRelationship pr = drawPP.getRelationship(picId); PackagePart imgPP = drawPP.getRelatedPart(pr); // byte imgBytes[] = IOUtils.toByteArray(imgPP.getInputStream()); System.out.println(imgPP.getPartName() +" - Col: "+oneAnc.getFrom().getCol() +" - Row: "+oneAnc.getFrom().getRow() ); } for (CTTwoCellAnchor twoAnc : draw.getWsDr().getTwoCellAnchorList()) { String picId = twoAnc.getPic().getBlipFill().getBlip().getEmbed(); PackageRelationship pr = drawPP.getRelationship(picId); PackagePart imgPP = drawPP.getRelatedPart(pr); System.out.println(imgPP.getPartName() +" - Col1: "+twoAnc.getFrom().getCol() +" - Row1: "+twoAnc.getFrom().getRow() +" - Col2: "+twoAnc.getTo().getCol() +" - Row2: "+twoAnc.getTo().getRow() ); } } opc.revert(); } }

更多推荐

本文发布于:2023-08-06 16:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   单元格   图像   Apache   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!