Excel 파일을 읽는 코드를 디버깅하려고하면 오류가 발생합니다. 'Microsoft.Office.Interop.Excel._Application'에 대한 참조가 잘못되었는지 궁금해서 다음 오류가 발생합니다.형식을로드 할 수 없습니다. Microsoft.Office.Interop.Excel._Application
은 Microsoft.Office.Interop.Excel
버전이 아니며 버전은 12일까요? 내 프로젝트 어셈블리에서로드되지 않았습니까?
유형 'Microsoft.Office.Interop.Excel._Application' 어셈블리에서 '대화, 버전 = 1.0.0.0, 중립 문화 = PublicKeyToken = null이'를로드 할 수 없습니다. 형식이 이고 형식이 이면 적합하지만 포함 된 어셈블리는 완전히 으로로드되지 않습니다.
코드를 디버깅하려고하면 기능에 들어가기 전에 예외가 발생합니다. 당신이 나쁜 참조, 나쁜 코드를 가지고있는 것처럼
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);
}
}
C# 4.0 컴파일러를 사용하는 경우 Interop 어셈블리 포함을 해제하십시오. – vcsjones