从 Excel xls HSSF 获取 Apache POI 中的图片位置 [英] Get picture position in Apache POI from Excel xls HSSF

查看:41
本文介绍了从 Excel xls HSSF 获取 Apache POI 中的图片位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以字节为单位获取图片数据,然后是锚点细节和finaly在屏幕上为我的用户显示目标位置和大小的结果...

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...

API 显示我们可以通过这样的工作簿对象获取所有图片的列表:

The API shows we can get a list of all pictures through workbook object like this:

List<HSSFPictureData> picturesData = workbook.getAllPictures();

而且我们还能够获取所有锚点的详细信息(请参阅 HSSFClientAnchor 详细信息 这里).

and also we are able to fetch all anchors details (see HSSFClientAnchor details here).

问题是锚点引用的 HSSFPicture.getPictureIndex() 与上述 getAllPictures() 方法的 HSSFPictureData 集合索引不匹配...

Problem is HSSFPicture.getPictureIndex() refered by an anchor does not match HSSFPictureData collection index of getAllPictures() method above...

...现在的问题是:如何将 HSSFClientAnchor 实例映射到特定的 HSSFPictureData 实例或相反?

...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);

            }


        }

注意:上面的代码会将图片数据从一张纸读取到另一张纸this.tSheet.sheet() 是源表this.newSheet 是新工作表

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆