This project is read-only.

How To Convert Shape to Image

Sep 14, 2016 at 3:40 AM
I have requirement to get all images including shapes from excel (.xls and .xlsx) and save it in directory, I've trying using HSSFWorkbook.GetAllPictures(), it does get all images but not the shapes, so i find another method and i found GetSheetAt(0).DrawingPatriarch.

here snippet of my code :
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;
    }
}
With that code I'm able to get all images and shapes that I want, but I can only Save ImageData (HSSFPicture) to directory, and I can't find any method to Save Shapes Data (HSSFSimpleShape) or convert it to Image.

Question is, how to convert HSSFSimpleShape data to Image? Is that Possible? Is there anything I miss?


Hope Someone can help, Thanks