How to access embedded OLE Objects to render document's preview image and extract original document file format using Aspose.Cells in .NET

Hi Team,

We have a use case where we have the following cell file
Parent.zip (43.7 KB)

that has two different worksheets each of which has a pptx and a word document embedded into it respectively.

These files are embedded in “preview” mode meaning that for each file a preview image is created inside the parent cell file. We need to access these image files.

For that we tried to use Worksheet.Pictures Property but unfortunately it doesn’t do the job, meaning that for each worksheet the Worksheet.Pictures property contains zero images.

Please advise on how to access these embedded preview images using Aspose.Cells for .NET.

Thank you,
Zoltan

@zpopswat,

Since your worksheets have embedded Ole objects, so you cannot get into Worksheet.Pictures. Please try the following sample code to accomplish your task. The following sample code will loop through ole objects (based on specific ole objects’ file type) in each sheet and then render two things: static image (as per your needs) and original file format.
e.g.
Sample code:

string SourceFile = "f:\\Files\\";
            // Open the template file. 
            Workbook workbook = new Workbook("e:\\test2\\Parent.xlsx");

            for (int z = 0; z < workbook.Worksheets.Count; z++)
            {
                // Get the OleObject Collection in each worksheet. 
                Aspose.Cells.Drawing.OleObjectCollection oles = workbook.Worksheets[z].OleObjects;
                // Loop through all the oleobjects and extract each object.
                // In the worksheet.
                for (int i = 0; i < oles.Count; i++)
                {
                    Aspose.Cells.Drawing.OleObject ole = oles[i];
                    string title = ole.Title;
                    string text = ole.Text;
                    string name = ole.Name;

                    // Specify the output filename. 
                    string fileName = SourceFile + "ole_" + i + ".";
                    fileName = SourceFile + ole.ObjectSourceFullName + i + ".";
                    // Specify each file format based on the oleobject format type. 
                    switch (ole.FileFormatType)
                    {
                        case FileFormatType.Doc:
                            fileName += "doc";
                            break;
                        case FileFormatType.Docx:
                            fileName += "docx";
                            break;
                        case FileFormatType.Xlsm:
                            fileName += "xlsm";
                            break;
                        case FileFormatType.Csv:
                            fileName += "csv";
                            break;
                        case FileFormatType.Xps:
                            fileName += "xps";
                            break;
                        case FileFormatType.Xlsx:
                            fileName += "xlsx";
                            break;
                        case FileFormatType.Ppt:
                            fileName += "ppt";
                            break;
                        case FileFormatType.Pptx:
                            fileName += "pptx";
                            break;
                        case FileFormatType.Vsd:
                            fileName += "vsd";
                            break;
                        case FileFormatType.Vsdx:
                            fileName += "vsdx";
                            break;
                        case FileFormatType.Pdf:
                            fileName += "Pdf";
                            break;
                        case FileFormatType.Unknown:
                            fileName += "Jpg";
                            break;
                        default:
                            //........ 
                            break;
                    }
                    // Save the oleobject as a new excel file if the object type is xls.
                    if (ole.FileFormatType == FileFormatType.Xlsx)
                    {
                        MemoryStream ms = new MemoryStream();
                        ms.Write(ole.ObjectData, 0, ole.ObjectData.Length);
                        Workbook oleBook = new Workbook(ms);
                        oleBook.Settings.IsHidden = false;
                        oleBook.Save(SourceFile + "Excel_File" + i + ".out.xlsx");
                    }
                    // Create the files based on the oleobject format types. 
                    else
                    {
                        //export image.
                        ole.ToImage(fileName + ".png", new ImageOrPrintOptions() { ImageType = ImageType.Png });
                        
                        //Export file.
                        FileStream fs = File.Create(fileName);
                        fs.Write(ole.ObjectData, 0, ole.ObjectData.Length);
                        fs.Close();
                    }
                }
            }

Also, see the document on managing Ole Objects in Excel spreadsheet for your reference.

Hope, this helps a bit.

1 Like

@zpopswat

You can also get the preview image from OleObject.ImageData directly.
Code:

Workbook wb = new Workbook("Parent.xlsx");
OleObjectCollection oles = wb.Worksheets[0].OleObjects;
OleObject ole = oles[0];
byte[] imageData = ole.ImageData;
//you can set file extension according to image type.
ImageType imageType = ole.ImageType;
using (FileStream fs = File.OpenWrite("testOle.emf"))
{
    fs.Write(imageData, 0, imageData.Length);
}
1 Like