不能够插入图像插入excelsheet [英] Not able to insert image into excelsheet

查看:160
本文介绍了不能够插入图像插入excelsheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入图像到Excel在Android中使用以下code,但没能做到这一点,请帮助!

//创建,我们将我们置于外部存储//对象列表的路径

 文件文件=新的文件(context.getExternalFilesDir(空),abc.xls);
    FileOutputStream中fileOS = NULL;
    //图像数据添加到该工作簿。
    InputStream为= text.getResources()getAssets()开(images.jpg)。;
    字节[]字节= IOUtils.toByteArray(是);
    INT pictureIdx = wb.addPicture(字节,Workbook.PICTURE_TYPE_JPEG);
    is.close();    CreationHelper帮手= wb.getCreationHelper();    //创建表
    苫布苫布= wb.createSheet();    //创建绘图族长。这是对于所有形状的顶层容器。
    图画= sheet.createDrawingPatriarch();    //添加图片形状
    ClientAnchor锚= helper.createClientAnchor();
    //画面的设定左上角,
    //图片#调整大小()的后续调用将操作相对于它
    anchor.setCol1(0);
    anchor.setRow1(0);
    图片PICT = drawing.createPicture(锚,pictureIdx);    //自动尺寸图象相对于它的左上角
// pict.resize();
//如果(WB的instanceof XSSFWorkbook)文件+ =X;
    fileOS =新的FileOutputStream(文件);
    wb.write(fileOS);


解决方案

按照此:

 的FileInputStream FIS =新的FileInputStream(的ImagePath);
        INT B:
        字节[]字节= IOUtils.toByteArray(FIS);
        fis.close();        //这将插入从开始的细胞图像结束的Excel细胞
        //表。
        HSSFClientAnchor锚=新HSSFClientAnchor(0,0,0,0,
                start.getCol(),start.getRow(),end.getCol(),end.getRow());        anchor.setAnchorType(2);        INT指数= wb.addPicture(字节,HSSFWorkbook.PICTURE_TYPE_JPEG);        //创建绘图族长。这是对于所有形状的顶层容器。
        绘制族长= sheet.createDrawingPatriarch();
        尝试{
            HSSFPicture照片= patriarch.createPicture(锚,指数);
            // picture.resize();
        }赶上(例外五){
            串ERR = e.getMessage();
        }

下面开始是图像的左上角的起始单元格引用。同样的,到底是图像的左上角结束单元格引用。

  CellReference启动;
CellReference结束;

和注意这一点。
    //创建绘图族长。这是对于所有形状的顶层容器。

 图画= sheet.createDrawingPatriarch();

警惕使用这种code的。在同一张图像插入的情况下,这个code创造了新的族长对每个要插入新图片/图像的时间。确保第二次图像插入此code选取为特定表插入的地方是必须要做的老族长对象。

Hi I am trying to insert image into excel in android, using the following code but not able to do so, please help !!

// Create a path where we will place our List of objects on external // storage

               File file = new File(context.getExternalFilesDir(null), "abc.xls");
    FileOutputStream fileOS = null;
    //add picture data to this workbook.
    InputStream is = text.getResources().getAssets().open("images.jpg");
    byte[] bytes = IOUtils.toByteArray(is);
    int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
    is.close();

    CreationHelper helper = wb.getCreationHelper();

    //create sheet
    Sheet sheet = wb.createSheet();

    // Create the drawing patriarch. This is the top level container for all shapes. 
    Drawing drawing = sheet.createDrawingPatriarch();

    //add a picture shape
    ClientAnchor anchor = helper.createClientAnchor();
    //set top-left corner of the picture,
    //subsequent call of Picture#resize() will operate relative to it
    anchor.setCol1(0);
    anchor.setRow1(0);
    Picture pict = drawing.createPicture(anchor, pictureIdx);

    //auto-size picture relative to its top-left corner
//  pict.resize();


//  if(wb instanceof XSSFWorkbook) file += "x";
    fileOS= new FileOutputStream(file);
    wb.write(fileOS);

解决方案

Follow this:

        FileInputStream fis = new FileInputStream(imagePath);
        int b;
        byte[] bytes = IOUtils.toByteArray(fis);
        fis.close();

        // This will insert the picture from start cell to end cell of excel
        // sheet.
        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
                start.getCol(), start.getRow(), end.getCol(), end.getRow());

        anchor.setAnchorType(2);

        int index = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);

        // Create the drawing patriarch. This is the top level container for all shapes. 
        Drawing patriarch = sheet.createDrawingPatriarch();
        try {
            HSSFPicture picture = patriarch.createPicture(anchor, index);
            // picture.resize();
        } catch (Exception e) {
            String err = e.getMessage();
        }

Here start is the starting cell reference for the left-top corner of image. Similarly, end is the ending cell reference for the left-top corner of image.

CellReference start;
CellReference end;

and Note this. // Create the drawing patriarch. This is the top level container for all shapes.

Drawing drawing = sheet.createDrawingPatriarch();

Be wary of using this code. In case of Image insertion in the same sheet this code creates new Patriarch every time for new picture/image to be inserted. Make sure that for the second time image insertion this code picks the old Patriarch object for the particular sheet where insertion is to be done.

这篇关于不能够插入图像插入excelsheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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