2014-01-17 5 views
0

이 문제점을 해결하는 데 어려움을 겪고 있습니다. SQL Server 데이터베이스로 가져와야하는 Excel 스프레드 시트가 있습니다. 다음과 같이 나는 그것을 반복 :System.DBNull을 int로 변환 할 수 없습니다.

Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.Range["A1", "D6867"]; 

int num = 4; 
// String test = ""; 
foreach (Microsoft.Office.Interop.Excel.Range row in xlRange.Rows) 
{ 
    if ((int)xlWorksheet.Cells[num, 1].Font.Size == 14) 
    { 
      ProductCategory category = new ProductCategory(); 
      category.Category = xlWorksheet.Cells[num, 1].Value.ToString(); 
      db.ProductCategories.Add(category); 
    } 
    num++; 
    //System.Diagnostics.Debug.WriteLine(test); 
} db.SaveChanges(); 
xlWorkbook.Close(true, Missing.Value, Missing.Value); 
xlApp.Quit(); 

내가지고있어 오류가

이 라인에서

을 int로 System.DBNull을 변환 할 수 없습니다 :

if ((int)xlWorksheet.Cells[num, 1].Font.Size == 14) 

이 오류의 의미가 무엇인지 알지 못하며 액세스중인 셀에 Null 값이 없습니다. 제발 조언 해 줘?

답변

3

적어도 하나의 셀에서 글꼴 크기는 System.DBNull입니다.

당신은 그것을 주조하기 전에 크기의 종류를 확인해야 :

if(Convert.IsDBNull(xlWorksheet.Cells[num, 1].Font.Size)) 
{ 
} 
else if((int)xlWorksheet.Cells[num, 1].Font.Size == 14) 
{ 
    // do Something.... 
} 
+3

내 대답 업데이트,'힌트 ta.speot.is에 대한 –

+0

감사 is'보다 조금 더 않음을 또한 Convert.IsDBNull있다 – David