打开使用 Microsoft Graph API 发送的电子邮件中的附件 Excel 文件时出错。错误 "Excel cannot open the file because the.."

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

您好,我正在使用 Microsoft Graph API 消息发送电子邮件。一切顺利,除了当文件附件是 excel (xlsx) 时,附件的电子邮件文件在打开文件时显示错误。
错误是“Excel 无法打开文件“Myfile.xlsx”,因为文件格式或文件扩展名无效。请确认文件未损坏且文件扩展名与文件格式匹配” .

这里要注意的是,我没有发布word文档,我可以毫无问题地打开它。此外,我认为没有权限问题,因为我有一个旧项目,它使用其他一些工具来发送电子邮件,当我使用该电子邮件收到附加的 excel 文件时,我没有看到任何问题。
代码:

    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;        
    }

如果您已经尝试了所有可能的方法来修复您的 Excel 文件但其中 none 有效,那么也许您的 Excel 文件没有损坏,但您只是没有有权在您的系统上打开它。

如果您没有权限打开 Excel 文件,可能会显示错误“Excel 无法打开文件,因为文件格式或文件扩展名无效。”我已经在 Windows 10 上对此进行了测试。您可以使用以下步骤授予此计算机上的每个人打开 Excel 文件的权限。

  1. Right-click无法打开的Excel文件和select 属性。

  2. 打开文件的“属性”对话框后,select“安全”选项卡, 然后单击“编辑”按钮。

  3. 打开文件的“权限”对话框后,单击“添加”按钮。

  4. Select 用户或组对话框打开后,单击高级 按钮。当下一个对话框打开时,单击立即查找,以便所有用户和 组将显示在底部的搜索结果列表中 对话。 Select 列表中的 Everyone 组,然后单击“确定”。 再次单击“确定”。

  5. 当您 return 到“权限”对话框时,您将看到“所有人” 组已添加到组或用户列表中。 Select 所有人 组,选中允许下的所有复选框,然后单击应用。

我希望这个答案能解决问题。

此致,

爱丽丝