none
How To Convert Shape to Image From Excel (xls and xlsx) using NPOI (C#) RRS feed

  • Question

  • I'm writting code to Get All Images including Shapes from Excel (.xls and xlsx) using C# and NPOI (ver 2.2.1) Library. The purpose is to get all images including shapes from selected Excel file and then save it to specified directory.

    After a while trial-error and browse any references, i'm able to get all Image from Excel using this code (I put code for read xls format),

            var lst = workbook.GetAllPictures();
            for (int i = 0; i < lst.Count; i++)
            {
                var pic = (HSSFPictureData)lst[i];
                byte[] data = pic.Data;
    
                /*Save Image From Byte[]*/
            }

    But, i can't get the Shapes in Excel with that Code, so i'm trying to find any other method and finally i found some code that can get List of Images and Shapes that exist in Sheet of Excel,

    here the snippet of code (also code for read xls format file),

    var dr = workbook.GetSheetAt(sht).DrawingPatriarch;
    HSSFPatriarch pat = (HSSFPatriarch)dr;
    var shape = pat.Children;
    int i = 0;
    foreach (var s in shape)
    {
        string patType = s.GetType().ToString();
        switch (patType)
        {
            case "NPOI.HSSF.UserModel.HSSFSimpleShape":
                {
                    var simpleshape = (HSSFSimpleShape)s;
    
                    /*Save Shape*/
    
                    break;
                }
            case "NPOI.HSSF.UserModel.HSSFPicture":
                {
                    var pic = (HSSFPicture)s;
                    byte[] data = pic.PictureData.Data;
    
                    /*Code to Save Image From Byte[]*/
    
                    break;
                }
            default: break;
        }
    }

    And when i put breakpoint on foreach statement and watch, it's show the list of Images and Shapes that i need to Save.

    With that code, I'm able to Get and Save its Image data (HSSFPicture), But I can't find any Method or Properties for Converting or Saving Shape Data (HSSFSimpleShape) to Image.

    My Question is, How can I Convert this Shape data to Image, and saving it to specified directory? Is there something wrong with the code that I write? Or maybe there is another solution (using NPOI of course) that i can use to get Shapes from Excel (xls or xlsx) file?

    i've been trying several other library so far:

    EPPlus, just Complatible with Excel Higher or Equal 2007 (xlsx), but I need to allow xls to, and EPPlus is give me error if i try to Convert uploaded xls format to xlsx using System.IO (I'll Post in another Question for This).

    Excel.Interrop, it can retrieve shape and image that i want, but it need Microsoft Office to be installed on server machine, and somehow its not an option for now.

    • Moved by CoolDadTx Wednesday, September 14, 2016 2:10 PM Office related
    Wednesday, September 14, 2016 2:48 AM

Answers

  • Hello,

     I found some information pertaining to your request.

    I do hope you take the time to READ the articles;

     http://joymonscode.blogspot.com/2009/01/reading-image-from-excel-file.html

     https://social.msdn.microsoft.com/Forums/office/en-US/6329ac07-7406-4549-bbed-5776f21c63cd/save-shapes-to-file-and-insert-editable-shapes-to-excel-later?forum=exceldev

     Hope this Helps :)

    Wednesday, September 14, 2016 10:16 AM

All replies

  • Hello,

     I found some information pertaining to your request.

    I do hope you take the time to READ the articles;

     http://joymonscode.blogspot.com/2009/01/reading-image-from-excel-file.html

     https://social.msdn.microsoft.com/Forums/office/en-US/6329ac07-7406-4549-bbed-5776f21c63cd/save-shapes-to-file-and-insert-editable-shapes-to-excel-later?forum=exceldev

     Hope this Helps :)

    Wednesday, September 14, 2016 10:16 AM
  • Hello

    Thanks, i'll try to look at that links, sorry for late reply,

    Thursday, September 15, 2016 1:02 AM
  • Hi Reggy,

     Can you share answer ...beacuse i am laso facing problem

    Friday, September 16, 2016 7:40 AM