Getting error when opening an attached Excel file from the email sent using Microsoft Graph API. Error "Excel cannot open the file because the.."

Viewed 155

Hi I am sending email using Microsoft Graph API message. All is going well except that when the file attachment is excel (xlsx) the attached email file shows an error upon opening up the file.
The error is "Excel cannot open the file "Myfile.xlsx" because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file".

The thing to note here, I do not gett issue the the word document and I am able to open it without any issue. Also I don't think there is an issue of permission since I have old project which uses some other tool for the email and when I received the attached excel file using that email I do not see an issue.
Code:

    private static MessageAttachmentsCollectionPage GetAttachments(List<HttpPostedFileBase> fileUploader)
    {           
        var attachmentPage = new MessageAttachmentsCollectionPage();
        if (fileUploader != null)
        {
            foreach (var file in fileUploader)
            {   
                var fileAttachment = new FileAttachment();
                fileAttachment.Name = file.FileName;
                //For xlsx content type is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                fileAttachment.ContentType = file.ContentType;
                fileAttachment.ContentBytes = ConvertToBytes(file);
                attachmentPage.Add(fileAttachment);
            }
        }

        return attachmentPage;
    }

    private static byte[] ConvertToBytes(HttpPostedFileBase item)
    {

        byte[] data;
        var bytes = new byte[item.InputStream.Length];
        item.InputStream.Position = 0;
        item.InputStream.Read(bytes, 0, bytes.Length);
        data = bytes;
        return data;        
    }
1 Answers

If you have tried all possible methods to repair your Excel file but none of them work, then maybe your Excel file is not corrupted, but you just don’t have permission to open it on your system.

If you don’t have permission to open the Excel file, it may display the error "Excel cannot open the file because the file format or file extension is not valid." I have tested this on Windows 10. You can use the following steps to grant everyone on this computer permission to open the Excel file.

  1. Right-click the Excel file that cannot be opened and select Properties.

  2. After the file’s Properties dialog opens, select the Security tab, and then click the Edit button.

  3. After the file’s Permissions dialog opens, click the Add button.

  4. When the Select Users or Groups dialog opens, click the Advanced button. When the next dialog opens, click Find Now so all users and groups will show up in the search results list at the bottom of the dialog. Select the Everyone group from the list, and then click OK. Click OK again.

  5. When you return to the Permissions dialog, you will see the Everyone group has been added to the group or user list. Select the Everyone group, check all checkboxes under Allow, and then click Apply.

I hope this answer will fix the issue.

Best Regards,

Alice

Related