gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:30:13 28 4
gpt4 key购买 nike

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

Microsoft.Office.Interop.Excel 版本不是应该是 12 吗?而不是从我的项目程序集中加载?

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.

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

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

最佳答案

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

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

关于c# - 无法加载类型 Microsoft.Office.Interop.Excel._Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5858089/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com