gpt4 book ai didi

java - Apache POI - 从单个工作表中获取图片

转载 作者:行者123 更新时间:2023-12-01 18:01:40 26 4
gpt4 key购买 nike

我正在尝试从单张纸加载图片,但是在 documentation 中只有如何从整个文档加载图片的示例(workbook.getAllPictures();)

最佳答案

我认为没有像 getAllPictures() 这样简单的东西可用于单个工作表。一种解决方案是执行以下操作(假设您正在使用 XSSF):

public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
XSSFSheet sheet1 = (XSSFSheet)wb.getSheetAt(0);

//returns the existing SpreadsheetDrawingML from the sheet, or creates a new one
XSSFDrawing drawing = sheet1.createDrawingPatriarch();

//loop through all of the shapes in the drawing area
for(XSSFShape shape : drawing.getShapes()){
if(shape instanceof Picture){
//convert the shape into a picture
XSSFPicture picture = (XSSFPicture)shape;

//your logic here
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

总体思路是,我们使用 createDrawingPatriarch() 从工作表中检索现有的 SpreadsheetML 绘图。

然后我们可以使用 getShapes() 来检索工作表中包含的每个形状。

关于java - Apache POI - 从单个工作表中获取图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com