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

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

问题描述

当我尝试调试读取Excel文件的代码时,我收到一个错误。我想知道如果引用Microsoft.Office.Interop.Excel._Application是错误的,我收到以下错误。



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


无法加载类型
'Microsoft.Office.Interop.Excel._Application'
来自程序集Dialog,
Version = 1.0.0.0,Culture = neutral,
PublicKeyToken = null'。该类型是
标记为符合类型
等价的资格,但$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ / b
$ b

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

  DataTable data = ExcelImport.GetDataFromFile(file); 

我的功能

 code> public static DataSet GetDataFromFile(string fileName)
{
应用程序oXL;
工作簿oWB;
工作表oSheet;
范围oRng;

//需要用excel文件解决文化问题。
CultureInfo cult = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(en-US);
try
{
//创建一个应用程序对象
oXL = new Application();
//获取WorkBook对象
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);

//获取WorkSheet对象
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;
//获取数据列
(int j = 1; j< = jValue; j ++)
{
dt.Columns.Add(column+ j,Type .GetType( System.String));
}

//获取单元格
中的数据(int i = 1; i = iValue; i ++)
{
var test = (范围)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 =邪教
return ds;
}
catch(Exception ex)
{
throw new Exception(Error reading excel file,ex);
}
}


解决方案

一个href =https://stackoverflow.com/users/492405/vcsjones> vcsjones 表示让我在正确的轨道上。关闭嵌入式互操作程序集后,我需要编辑web.config来信任Excel

 < system.web> 
< trust level =FulloriginUrl =/>


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天全站免登陆