使用Apache POI获取.xlsx文件中图像的图像位置/位置

时间:2015-04-16 10:19:11

标签: java excel apache-poi

我只能通过此方法获取图像数据.getAllPictures()

List<XSSFPictureData> lst = (List)workbook.getAllPictures();

每当我使用getShapes()

时,我都会得到这个
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
at org.codehaus.groovy.tools.javac.JavacJavaCompiler.addJavacError(JavacJavaCompiler.java:92)
at org.codehaus.groovy.tools.javac.JavacJavaCompiler.compile(JavacJavaCompiler.java:71)
at org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit.gotoPhase(JavaAwareCompilationUnit.java:97)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:529)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:498)
at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:55)
at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:210)
at org.codehaus.groovy.ant.Groovyc.runCompiler(Groovyc.java:1078)
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:1129)
at org.codehaus.groovy.ant.Groovyc.execute(Groovyc.java:748)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)

2015-04-21 09:54:36,457 ERROR StackTrace全栈跟踪: org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:

是否可以或者有办法让我在.xlsx文件中获取图像位置/位置?

1 个答案:

答案 0 :(得分:3)

您可以通过XSSFPicture.getClientAnchor()方法检索位置,ClientAnchor有一个行/列位置和一个像素偏移量,它应该允许获取您要查找的信息,例如:

XSSFDrawing dp = wb.getSheetAt(0).createDrawingPatriarch();
List<XSSFShape> pics = dp.getShapes();
XSSFPicture inpPic = (XSSFPicture)pics.get(0);

XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();

System.out.println("col1: " + clientAnchor.getCol1() + ", col2: " + clientAnchor.getCol2() + ", row1: " + clientAnchor.getRow1() + ", row2: " + clientAnchor.getRow2());
System.out.println("x1: " + clientAnchor.getDx1() + ", x2: " + clientAnchor.getDx2() +  ", y1: " + clientAnchor.getDy1() +  ", y2: " + clientAnchor.getDy2());

将打印出类似

的内容
col1: 0, col2: 2, row1: 1, row2: 7
x1: 252000, x2: 63720, y1: 60480, y2: 201600