我想在一个文件中读取多个Excel表而不使用C#中的OLEDB

即时通讯从C#Windows窗体读取Excel文档..有25个工作表在Excel工作簿..我可以读取第一个工作表成功..但是,当我将其更改为工作表2 ..它不会在所有工作..林没有使用OLEDB ..

我想在每张纸上读100行..以下是我的代码…

` 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;

希望你会知道答案..

试试这个:

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

Sheets属性可以包含非工作表表单,这可能是您的scheme中的问题。