How to get Attachment in Excel sheet

Hi @asad.ali ,

       I have a sceanrio where my .xlsx excel sheet is having .msg as a atttachment, But the Issue is my code does not extract the child attachment. On contrary if i have .eml file with .xlsx attachment in it , it is extracted success fully. 

Can you share me the Code snippet of how to Extract xlsx file child Object ??

Currently i have below Code snippet which does not execute switch statement coz it comes out since This condition becomes true.

!oleObject.DisplayAsIcon

also note when i try to see Extension valeu of (oleObject.FileFormatType) it shows Ole10Native instead of msg .

public override List GetDirectChilds()
{
using (var workbook = LoadWorkbook())
{
var childs = new List();

            foreach (var worksheet in workbook.Worksheets)
            foreach (var oleObject in worksheet.OleObjects)
            {
                if (oleObject.IsLink || !oleObject.DisplayAsIcon)
                {
                    continue;
                }

                string extension;
                switch (oleObject.FileFormatType)
                {
                    case FileFormatType.MsEquation:
                        continue;
                    case FileFormatType.MapiMessage:
                        extension = ".msg";
                        break;
                    case FileFormatType.Unknown:
                        extension = Path.GetExtension(oleObject.ObjectSourceFullName);
                        break;
                    default:
                        extension = string.Format(".{0}", oleObject.FileFormatType.ToString().ToLower());

                        if (!IsSupported(extension))
                        {
                            throw new BadRequestException($"No converter found for file extension: {extension}");
                        }

                        break;
                }

                childs.Add(new DirectChild(extension, true, new MemoryStream(oleObject.ObjectData),
                    originalFileName: oleObject.ObjectSourceFullName));
            }

            return childs;
        }
    }\\\Kindly do not ask me to share the Whole code snippet as here i am not asking you for any fix and i am just asking to Provide me solution so that i can get msg extracted from the xlsx file .

Kindly you provide me the Code snippet where it will show Correctly that this extension is of type msg and not ole10native type

@GayatriNaik
1,If the type of embedded file is ole10native type,please check OleObject.SourceFullName property.
2,Could share your template file here? We will check it soon.

Hi I am Unable to add attachment here . But i have shared steps and how it looks like
Its a normal xlsx sheet containing

  1. text as Test data
  2. Embedded Test.msg file

In Second Pic , i have share how the Outlook msg looks like its Simplet email containing Test message . Thats it .

Embedded File Created 2.png (25.7 KB)

Embedded File Created.png (49.0 KB)

hO tHANKS FOR pROVIDING HINT , i AM NOW ABLE TO CONVERT AND GET THE ATTACHMENTS TOO But Just wondering What was the significance of 1) !oleObject.DisplayAsIcon
2) oleoleObject.FileFormatType which is used in switch statement in above code. Please reply yas i need to comment out this code and i need confiramtion before doing so.

@GayatriNaik,

DisplayAsIcon - This Boolean attributes is used to check whether the specified (OLE) object is displayed as an icon or not. You may also set the specified OLE object as an icon.

@GayatriNaik
MS Excel embed the msg attachment as Ole10Native file now,so MS Excel will call the corresponding program to open the file based on the file’s suffix name.
We do not support further detection the content types of Ole10Native object.
Please check it as the following:

OleObject oleObject = workbook.Worksheets[0].OleObjects[0];
byte[] data = oleObject.ObjectData;
if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
//1, check the file extension as MS Excel
    string fileName = oleObject.ObjectSourceFullName;
   string extension = Path.GetExtension(fileName);
    //2, detect the file type by the content with  FileFormatUtil.DetectFileFormat
   // FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(new MemoryStream(data));
}

Hi Please note there is a bug in above code which u have shared coz

if we embed bmp file in excel sheet and try to convert , Then
below code will throwexception coz
oleObject.ObjectSourceFullName is comming as null for BMP files embedded in excel sheet.
Please try thsiscenario and revert how to handle it.

if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
//1, check the file extension as MS Excel
string fileName = oleObject.ObjectSourceFullName;
string extension = Path.GetExtension(fileName);
//2, detect the file type by the content with FileFormatUtil.DetectFileFormat
// FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(new MemoryStream(data));
}

@GayatriNaik,

I tested your scenario/case using a sample Excel file (it contains a BMP image as embedded object), it work absolutely fine. Please find attached (below) the template file for your reference. I am using our latest version/fix: Aspose.Cells for .NET v24.5 (please try it if you are not already using it).
e.g.,
Sample code:

Workbook workbook =  new Workbook("e:\\test2\\tst_Book1.xlsx");
OleObject oleObject = workbook.Worksheets[0].OleObjects[0];
byte[] data = oleObject.ObjectData;
if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
  Console.WriteLine(oleObject.ObjectSourceFullName);
} 

output:

C:\Users\hp\AppData\Local\Temp{3056B93A-64CE-424E-B106-75CDE1F90306}{37192006-3AE7-4FFA-9930-F0C6C9790C5A}\test1.bmp

file1.zip (15.2 KB)

But there is one more Major issue i cansee is if u have xlsx and then Insert Object
( insert any file eg text file ) now check checkbox Link to file option as shown in b elow screenshot
so in this case we get oledb.ObjectData as null, as a result we cannot convert it to the
PDFA and it crashes.

Please reply as this is limitation we can see and cannot extract embedded document

Note : Aspsoe.Cell = 22.50 and Aspose.PDF = 24.5.1

image.png (27.3 KB)

@GayatriNaik,

Yes, it is expected as in that case the object is linked (to its source) only and is not embedded into Excel workbook. Please note, you cannot obtain the OLE object data if it is linked to a file, so, OleObject.ObjectData is null. By the way, you can choose to get path from OleObject.ObjectSourceFullName and obtain the source data/object by yourselves, and then extract/save it.

Hi IT ried to Update aspose.cellfrom 22.5.0 to 24.5.0 and when i convert excel sheet with BMf file i still get
oleObject.ObjectSourceFullName as null in below code. Please suggest if i need to Upgrade any other Package too ??

if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
Console.WriteLine(oleObject.ObjectSourceFullName);
}

@GayatriNaik,

Please zip your Excel file and attach it here. We will check your issue soon.

image.png (86.6 KB)

EmbeddedExcelWithBMP.zip (28.1 KB)

Added error where i am getting null as mentioned above , and adding Zio for the tested Excel sheet

@GayatriNaik,

Thanks for the template Excel file.

After an initial test, I was able to reproduce the issue as you mentioned by using the following sample code with your Excel file. I found OleObject.ObjectSourceFullName is null for an embedded image in the worksheet.

Workbook workbook =  new Workbook("e:\\test2\\EmbeddedExcelWithBMP.xlsx");
OleObject oleObject = workbook.Worksheets[0].OleObjects[0];
byte[] data = oleObject.ObjectData;
if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
   Console.WriteLine(oleObject.ObjectData.Length);
   Console.WriteLine(oleObject.ObjectSourceFullName);//null
}

We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-55768

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@GayatriNaik
After closely looking into Ole10Native file type, we find that Ole10Native has two formats: one with a file name and the other without a file name.So please detect the content if the file name is null as the following:

Workbook d = new Workbook(dir + “EmbeddedExcelWithBMP.xlsx”);
foreach(Worksheet sheet in d.Worksheets)
{
foreach(OleObject o in sheet.OleObjects)
{
Console.WriteLine(o.FileFormatType);
Console.WriteLine(o.ObjectSourceFullName);
Console.WriteLine(o.IsLink);
if(string.IsNullOrEmpty(o.ObjectSourceFullName))
{
Console.WriteLine(FileFormatUtil.DetectFileFormat(new MemoryStream(o.ObjectData)).FileFormatType);
}
}
}

@GayatriNaik
In the next version, OleObject.FileFormatType will return BMP if the Ole10Native object contains a bmp without file name. We cannot support all file formats, so please share a template Excel file with all your expected files as Ole object, then we can check them soon.