2012-10-19 2 views
0

C# Windows Form에서 Excel 문서를 읽는 중 ... Excel 통합 문서에 25 개의 워크 시트가 있습니다. 첫 번째 워크 시트를 성공적으로 읽을 수 있습니다. 그것은 ... 다음은 내 코드입니다 .. C에서 OLEDB를 사용하지 않고 하나의 파일에서 여러 Excel 시트를 읽고 싶습니다.

내가 모든 시트에 100 행을 읽을 수 ..이 임 OLEDB를 사용하지 않는 .. 전혀 작동하지 않습니다 ..이 워크 시트에

`  dt.Columns.Add("Amount", typeof(double)); 
     dt.Columns.Add("ChequeNo", typeof(int)); 
     dt.Columns.Add("month", typeof(int)); 


     int AmountRow = 100; 
     int ChequeNoRow = 101; 
     int Column = 3; 

     xlApp = new Excel.ApplicationClass(); 
     xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[2];\\This place is the changing worksheets 

     range = xlWorkSheet.UsedRange; 

     double chequeAmount; 
     double chequeNo; 

     for (int i = Column; i < 15; i++) 
     { 
      chequeAmount = (double)(range.Cells[AmountRow, i] as Excel.Range).Value2; 
      chequeNo = (double)(range.Cells[ChequeNoRow, i] as Excel.Range).Value2; 

      if (chequeNo != 0.0) 
      { 
       dt.Rows.Add(Convert.ToDouble(chequeAmount), Convert.ToInt32(chequeNo), i); 
      } 
     } 

     dataGridView1.DataSource = dt; 
     xlWorkBook.Close(true, null, null); 
     xlApp.Quit(); 

     releaseObject(xlWorkSheet); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp);` 

releaseObject 메소드는 여기에 없습니다. 완벽하게 작동하는 것들은 ...

`xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];` 
,

이 다음 줄은 예외를 제공합니다 .. 난 내 워크 시트를 변경하는 방법입니다 .. [널 포인트 예외]

chequeAmount = (double)(range.Cells[AmountRow, i] as Excel.Range).Value2;

당신이 답을 알고 있습니다 희망 ..

+0

대신 [Linq-To-Excel] (http://code.google.com/p/linqtoexcel/)을 사용하십시오. 그것은 절대적으로 굉장합니다. – Enigmativity

+0

몇 가지 예외 형제가있었습니다 ... 우리는 전혀 LinQ를 사용하지 말라고 조언했습니다. 문제는 .. .. thankz 감사합니다. – tharindlaksh

+0

그건 이상한 요구 사항 인 것 같습니다. LINQ를 사용할 수없는 이유는 무엇입니까? – Enigmativity

답변

5

대신을 시도해보십시오

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[2];

Sheets 속성은 시나리오에서 문제가 될 수 비 워크 시트를 포함 할 수 있습니다.

+0

시간 주셔서 감사합니다 ..하지만이 작동하지 않습니다 ... : D 조 – tharindlaksh