无法加载类型Microsoft.Office.Interop.Excel._Application

时间:2011-05-02 14:00:47

标签: c# excel interop

当我尝试调试读取Excel文件的代码时出现错误。我想知道对'Microsoft.Office.Interop.Excel._Application'的引用是否错误,我收到以下错误。

不是假设为12的Microsoft.Office.Interop.Excel版本?而不是从我的项目组装中加载?

  

无法加载类型   'Microsoft.Office.Interop.Excel._Application'   从汇编'对话,   版本= 1.0.0.0,文化=中立,   公钥=空”。类型是   标记为有资格获得类型   等价,但含有   程序集未完全加载   可信的。

当我尝试调试代码时,我会在进入函数之前得到异常。

DataTable data = ExcelImport.GetDataFromFile(file);

我的功能

public static DataSet GetDataFromFile(string fileName)
    {
        Application oXL;
        Workbook oWB;
        Worksheet oSheet;
        Range oRng;

        // Needed to fix culture problem with excel files.
        CultureInfo cult = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        try
        {
            //  creat a Application object
            oXL = new Application();
            //   get   WorkBook  object
            oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);

            //   get   WorkSheet object
            oSheet = (Worksheet)oWB.Sheets[1];
            System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            //StringBuilder sb = new StringBuilder();
            int jValue = oSheet.UsedRange.Cells.Columns.Count;
            int iValue = oSheet.UsedRange.Cells.Rows.Count;
            //  get data columns
            for (int j = 1; j <= jValue; j++)
            {
                dt.Columns.Add("column" + j, Type.GetType("System.String"));
            }

            //  get data in cell
            for (int i = 1; i <= iValue; i++)
            {
                var test = (Range)oSheet.Cells[i, 1];

                if (!string.IsNullOrEmpty(test.Text))
                {
                    DataRow dr = ds.Tables["dtExcel"].NewRow();
                    for (int j = 1; j <= jValue; j++)
                    {
                        oRng = (Range)oSheet.Cells[i, j];
                        string strValue = oRng.Text.ToString();
                        dr["column" + j] = strValue;
                    }
                    ds.Tables["dtExcel"].Rows.Add(dr);
                }
            }
            oXL.Workbooks.Close();
            System.Threading.Thread.CurrentThread.CurrentCulture = cult;
            return ds;
        }
        catch (Exception ex)
        {
            throw new Exception("Error reading excel file", ex);
        }
    }

2 个答案:

答案 0 :(得分:2)

vcsjones赞扬让我走上了正确的轨道。关闭嵌入互操作程序集后,我需要编辑web.config以信任Excel

<system.web>
    <trust level="Full" originUrl="" />

答案 1 :(得分:1)

听起来你的参考不好,而不是糟糕的代码。这段代码有用吗?您是否尝试删除对Excel Interop的引用并重新添加它?因为它甚至没有进入你的方法,我的假设是,当系统试图加载引用dll时,它会崩溃。