是否可以使用Apache POI从xls电子表格中提取图像信息?
在我的一个项目中,我需要从.xls文件中读取一些图像。我可以一起阅读所有图像,但是如何获得图像位置(如列和行数或坐标)?否则我可以获得图像位置但我无法知道关于找到位置的特定图像的信息,如图片名称或扩展名或其他信息。我如何才能获得图像和位置?
答案 0 :(得分:4)
看看这里:
http://poi.apache.org/components/spreadsheet/quick-guide.html#Images
样品:
List lst = workbook.getAllPictures();
for (Iterator it = lst.iterator(); it.hasNext(); ) {
PictureData pict = (PictureData)it.next();
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("jpeg")) {
FileOutputStream out = new FileOutputStream("pict.jpg");
out.write(data);
out.close();
}
}
在此之后,您可以使用像ImageInfo这样的工具来扩展Magick以找出各种配置。哟甚至可以将图像转换为不同的大小。
看看这堂课:
http://blog.jaimon.co.uk/simpleimageinfo/SimpleImageInfo.java.html
- 希望这会有所帮助
答案 1 :(得分:2)
我希望这段代码会有所帮助)
XSSFDrawing dp = workbook.getSheetAt(1).createDrawingPatriarch();
List<XSSFShape> pics = dp.getShapes();
XSSFPicture inpPic = (XSSFPicture)pics.get(0);
XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();
inpPic.getShapeName(); // узнаю название картинки
PictureData pict = inpPic.getPictureData();
FileOutputStream out = new FileOutputStream("pict.jpg");
byte[] data = pict.getData();
out.write(data);
out.close();
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());
答案 2 :(得分:0)
如果你不想使用迭代器(因为有些时候它们很重)
public List readDrawing(Workbook workbook) throws Exception {
List list = workbook.getAllPictures();
for (int i=0; i<list.size(); i++) {
PictureData picture = (PictureData) list.get(i);
String ext = picture.suggestFileExtension();
byte[] data = picture.getData();
FileOutputStream out = new FileOutputStream("imageName" + "." + ext);
out.write(data);
out.close();
}
return list;
}