Get picture position in Apache POI from Excel xls HSSF

北城以北 提交于 2019-12-24 02:25:28

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/9232844/get-picture-position-in-apache-poi-from-excel-xls-hssf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!