[英]Read images from .xls file together with the references for their locations
在我的一個項目中,我需要從.xls
文件中讀取圖像。 對於每一行,有一列包含我需要讀出的圖像。
看起來我可以一起閱讀所有圖像,但是我怎樣才能獲得每個圖像的位置,例如列號和行號,以便我可以將這些圖像與其他數據相關聯?
只要形狀是圖片,以下都是可能的:
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
class GetShapePosition {
public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("workbook.xls");
Workbook wb = WorkbookFactory.create(inp);
HSSFSheet sheet = (HSSFSheet)wb.getSheetAt(0);
HSSFPatriarch dravingPatriarch = sheet.getDrawingPatriarch();
java.util.List<HSSFShape> shapes = dravingPatriarch.getChildren();
for (HSSFShape shape : shapes) {
if (shape instanceof HSSFPicture) {
HSSFPicture hssfPicture = (HSSFPicture)shape;
int picIndex = hssfPicture.getPictureIndex();
String filename = hssfPicture.getFileName();
int row = hssfPicture.getClientAnchor().getRow1();
int col = hssfPicture.getClientAnchor().getCol1();
System.out.println("Picture " + picIndex + " with Filename: " + filename + " is located row: " + row + ", col: " + col);
}
}
} catch (InvalidFormatException ifex) {
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.