You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#程序导入邮件信息到Excel时引入Interop.Excel后运行失败求助

解决Office Interop Excel引入后程序无法运行的问题

针对你遇到的引入Microsoft.Office.Interop.Excel后程序无法运行的问题,结合你的代码,整理了以下几个常见原因及解决办法:

1. 缺失必要的命名空间引用

你的代码中使用了Outlook相关对象(NameSpace、MAPIFolder等)和正则表达式,但未显式引用对应命名空间,会导致编译或运行报错。需在代码顶部添加:

using Microsoft.Office.Interop.Outlook;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices; // 用于释放COM对象

2. Office版本与Interop包不兼容

确保你安装的NuGet包版本(Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Outlook)与本地安装的Office版本一致。例如本地装的是Office 2021,就不要使用对应Office 2016的Interop包;若使用Office 365 ClickToRun版本,优先选择NuGet上标记为“兼容Office 365”的Interop包。

3. 权限与访问限制

  • Excel文件权限:确认程序对C:\Users\Ty\Documents\Returns.xlsx有读写权限,若文件已被其他程序打开,也会导致无法访问。
  • Outlook MAPI访问权限:首次运行时Outlook可能会弹出“是否允许此程序访问你的邮件”的提示,需手动点击允许;若未弹出,可在Outlook的「文件-选项-信任中心-信任中心设置-程序访问」中启用对应权限。

4. 未正确释放Office COM对象

Office Interop基于COM对象,若不手动释放会导致进程残留,进而引发后续运行错误。需在代码逻辑结束后添加资源释放代码:

// 释放Outlook相关对象
if (email != null) Marshal.ReleaseComObject(email);
if (folder != null) Marshal.ReleaseComObject(folder);
if (ns != null) Marshal.ReleaseComObject(ns);
if (outlook != null) Marshal.ReleaseComObject(outlook);

// 保存并释放Excel相关对象
workbook.Save();
workbook.Close();
if (sheet != null) Marshal.ReleaseComObject(sheet);
if (workbook != null) Marshal.ReleaseComObject(workbook);
excel.Quit();
if (excel != null) Marshal.ReleaseComObject(excel);

5. 编译架构不匹配

Office默认是32位安装,若你的程序编译平台设为x64或Any CPU(优先64位),会导致Interop调用失败。需在项目属性中把编译平台改为x86(与Office位数一致)。

修正后的完整代码示例

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Outlook;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

class Program
{
    static void Main()
    {
        Application excel = new Application();
        excel.Visible = true;
        Workbook workbook = excel.Workbooks.Open(@"C:\Users\Ty\Documents\Returns.xlsx");
        Worksheet sheet = (Worksheet)workbook.Worksheets[1];

        Outlook.Application outlook = new Outlook.Application();
        NameSpace ns = outlook.GetNamespace("MAPI");
        MAPIFolder folder = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
        MailItem email = folder.Items[1] as MailItem;

        // 提取姓名
        string pattern = @"Full Name:\s*(.*)";
        Match match = Regex.Match(email.Body, pattern);
        string name = match.Success ? match.Groups[1].Value : "";

        // 写入Excel
        int row = sheet.UsedRange.Rows.Count + 1;
        sheet.Cells[row, 1] = email.Subject;
        sheet.Cells[row, 2] = email.Body;
        sheet.Cells[row, 3] = name;

        // 释放资源
        if (email != null) Marshal.ReleaseComObject(email);
        if (folder != null) Marshal.ReleaseComObject(folder);
        if (ns != null) Marshal.ReleaseComObject(ns);
        if (outlook != null) Marshal.ReleaseComObject(outlook);

        workbook.Save();
        workbook.Close();
        if (sheet != null) Marshal.ReleaseComObject(sheet);
        if (workbook != null) Marshal.ReleaseComObject(workbook);
        excel.Quit();
        if (excel != null) Marshal.ReleaseComObject(excel);
    }
}

内容的提问来源于stack exchange,提问作者Ty Vents

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 18:21:34