Java 如何使用ApachePOI检查单元格是否包含图像?
我有以下代码块:Java 如何使用ApachePOI检查单元格是否包含图像?,java,image,excel,apache-poi,Java,Image,Excel,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)
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();
我正在使用ApachePOI读取.xlsx文件。每行可以包含一个单元格,其中包含用户粘贴到其中的图像
我希望抓取图像并将其编码到base64。如何在遍历电子表格时检查单元格是否包含图像
我已经读过了,但这涉及到首先获取所有图像,然后遍历它们。我想遍历单元格并检查图像。下面的代码遍历第一页的所有图像。 因此,您不需要遍历行/列,而是直接获得位置。 除了和,还有一些不能直接与细胞联系的
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();
}
}
在Excel中,形状永远不会“在”单元格中-它们只定位在单元格上方。不熟悉POI,但在VBA中,形状具有TopLeftCell属性,可用于确定它们“位于”的单元格,这在我检查电子表格时是有意义的。感谢您的澄清。您可以使用maven进行构建和测试:<代码>org.apache.poi poi 3.12-beta1 org.apache.poi ooxml 3.12-beta1 org.apache.poi poi ooxml模式3.12-beta1 org.apache.poi ooxml模式1.1