未能加载类型Microsoft.Office.Interop.Excel._Application [英] Could not load type Microsoft.Office.Interop.Excel._Application

查看:1076
本文介绍了未能加载类型Microsoft.Office.Interop.Excel._Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误,当我尝试调试读取Excel文件代码。我想知道,如果提及Microsoft.Office.Interop.Excel._Application'是错误的,我碰到下面的错误。



不是的Microsoft.Office.Interop.Excel 版本假设是12?而不是从我的项目集装?




未能加载类型
'Microsoft.Office.Interop.Excel._Application'
从程序集对话框,
版= 1.0.0.0,文化=中立,
公钥=空'。该类型是
标记为类型
等价为合格,但包含
组件未加载完全
信赖。




当我尝试调试代码,我得到的异常之前,我可以进入该功能。

  DataTable的数据= ExcelImport.GetDataFromFile(文件); 



我的功能



 公共静态数据集GetDataFromFile(字符串文件名)
{
应用OXL;
工作簿OWB;
表oSheet;
范围ORNG;

//所需解决文化问题的Excel文件。
CultureInfo的邪教= System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture =新的CultureInfo(EN-US);

{
//创造一​​个应用程序对象
OXL =新的应用程序();
//获取簿对象
OWB = oXL.Workbooks.Open(文件名,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);

//获取工作表对象
oSheet =(表)oWB.Sheets [1];
System.Data.DataTable DT =新System.Data.DataTable(dtExcel);
的DataSet DS =新的DataSet();
ds.Tables.Add(DT);

// StringBuilder的SB =新的StringBuilder();
INT jValue = oSheet.UsedRange.Cells.Columns.Count;
INT iValue = oSheet.UsedRange.Cells.Rows.Count;
//获取数据列
为(INT J = 1; J< = jValue; J ++)
{
dt.Columns.Add(列+ J,类型.GetType(System.String));
}

//获取细胞
数据的for(int i = 1; I< = iValue;我++)
{
VAR测试= (范围)oSheet.Cells [1,1]。

如果
{
博士的DataRow = ds.Tables [dtExcel] NEWROW()(string.IsNullOrEmpty(test.Text)!)。
为(INT J = 1; J< = jValue; J ++)
{
ORNG =(范围)oSheet.Cells [I,J]。
字符串strValue的= oRng.Text.ToString();
博士[一栏中+ J] = strValue的;
}
ds.Tables [dtExcel] Rows.Add(DR)。
}
}
oXL.Workbooks.Close();
System.Threading.Thread.CurrentThread.CurrentCulture =邪教;
返回DS;
}
赶上(异常前)
{
抛出新的异常(错误读取Excel文件,前);
}
}


解决方案

< A HREF =htt​​p://stackoverflow.com/users/492405/vcsjones> vcsjones 表彰了我正确的轨道上。关闭嵌入互操作程序集后,我需要编辑web.config信任的Excel

 <&的System.Web GT; 
<信任级别=全originUrl =/>


I'm getting a error when I try to debug a code that reads Excel files. I'm wondering if the reference to 'Microsoft.Office.Interop.Excel._Application' is wrong, I get the following error.

isn't the Microsoft.Office.Interop.Excel version suppose to be 12? and not loaded from my project assembly?

Could not load type 'Microsoft.Office.Interop.Excel._Application' from assembly 'Dialog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The type is marked as eligible for type equivalence, but the containing assembly is not loaded as fully trusted.

When I try to debug the code I get the exception before I can step into the function.

DataTable data = ExcelImport.GetDataFromFile(file);

My function

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

解决方案

vcsjones commend got me on the correct track. after turning off Embedding Interop Assemblies I needed to edit the web.config to trust Excel

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

这篇关于未能加载类型Microsoft.Office.Interop.Excel._Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆