2011-05-02 2 views
3

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

C# 4.0 컴파일러를 사용하는 경우 Interop 어셈블리 포함을 해제하십시오. – vcsjones

답변

2

vcsjones 올바른 트랙에 나를 붙 잡았습니다. Interop 어셈블리 포함 해제 후 Excel을 신뢰하도록 web.config를 편집해야했습니다.

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

다행이었습니다. 필자는 Office PIA를 임베디드 Interop 어셈블리로 사용하는 데 어려움을 겪었습니다. 어쨌든 대부분의 시스템에 GAC가 포함될 것이므로 Office PIA의 기능을 해제하는 것이 가장 좋습니다. – vcsjones

1

소리가 난다. 이 코드가 작동 한 적이 있습니까? Excel Interop에 대한 참조를 제거하고 다시 추가해 보셨습니까? 그것은 당신의 방법에 들어 가지 않기 때문에, 내 가정은 시스템이 참조 dll을로드하려고 할 때 충돌합니다.

관련 문제