从Excel XLS HSSF获取了Apache POI图片位置 [英] Get picture position in Apache POI from Excel xls HSSF
问题描述
我需要的是得到一个图像数据作为字节,那么它的锚细节和finaly
在我的用户目标位置和尺寸在屏幕上显示的结果...
该API显示了我们可以通过类似这样的工作簿对象中获取的所有照片的列表:
列表< HSSFPictureData> picturesData = workbook.getAllPictures();
,也是我们能够获取所有锚的详细信息(见HSSFClientAnchor细节这里)。
问题是由锚所指向不匹配getAllPictures的HSSFPictureData集合索引()方法上面...
HSSFPicture.getPictureIndex()...现在的问题是:如何HSSFClientAnchor实例映射到特定HSSFPictureData实例或相反?
列表< HSSFShape>形状= this.tSheet.sheet()getDrawingPatriarch()的getChildren()。
的for(int i = 0; I< shapes.size();我++)
{
如果(shapes.get(I)的instanceof HSSFPicture)
{
HSSFPicture PIC =(HSSFPicture)shapes.get(ⅰ);
。HSSFPictureData picdata = this.tSheet.sheet()getWorkbook()getAllPictures()获得(pic.getPictureIndex());
INT pictureIndex = this.newSheet.getWorkbook()给AddPicture(picdata.getData(),picdata.getFormat())。this.newSheet.createDrawingPatriarch()createPicture((HSSFClientAnchor)pic.getAnchor()R,pictureIndex)。 }
}
请注意:
上述code将一张图片读取数据到另一个表
this.tSheet.sheet()是源表
this.newSheet是新的工作表
My need is to get a picture data as bytes, then it's anchor details and finaly display the result on screen at target position and size for my users...
The API shows we can get a list of all pictures through workbook object like this:
List<HSSFPictureData> picturesData = workbook.getAllPictures();
and also we are able to fetch all anchors details (see HSSFClientAnchor details here).
Problem is HSSFPicture.getPictureIndex() refered by an anchor does not match HSSFPictureData collection index of getAllPictures() method above...
...now the question is: how to map HSSFClientAnchor instances to specific HSSFPictureData instances or the opposite?
List<HSSFShape> shapes = this.tSheet.sheet().getDrawingPatriarch().getChildren();
for (int i = 0; i < shapes.size(); i++)
{
if(shapes.get(i) instanceof HSSFPicture)
{
HSSFPicture pic = (HSSFPicture) shapes.get(i);
HSSFPictureData picdata = this.tSheet.sheet().getWorkbook().getAllPictures().get(pic.getPictureIndex());
int pictureIndex = this.newSheet.getWorkbook().addPicture( picdata.getData(), picdata.getFormat());
this.newSheet.createDrawingPatriarch().createPicture((HSSFClientAnchor)pic.getAnchor()r, pictureIndex);
}
}
NOTE: Above code will read pic data from one sheet to another sheet this.tSheet.sheet() is source sheet this.newSheet is new sheet
这篇关于从Excel XLS HSSF获取了Apache POI图片位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!