我想從Excel中的文件讀取數據,但出於某種原因出錯。這是我在做什麼:如何從Excel文件讀取數據?
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
Excel.Range range ;
string str;
int rCnt ;
int cCnt ;
int rw = 0;
int cl = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\pc\Desktop\Alessio.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
for (rCnt = 1; rCnt <= rw; rCnt++)
{
for (cCnt = 1; cCnt <= cl; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
MessageBox.Show(str);
}
}
,這是例外,我得到:
"System.Runtime.InteropServices.COMException' in WindowsFormsApplication2.exe"
Adding information:HRESULT: 0x80010105 (RPC_E_SERVERFAULT)
你知道爲什麼,我該如何解決這個問題?
運行時錯誤在哪裏?嘗試使用F10進行調試並查看地點。與此同時,嘗試:'str = range.Cells [rCnt,cCnt] .Value2;' – Vityata
當我嘗試執行下面的代碼行時,我得到異常:「xlWorkBook = xlApp.Workbooks.Open(@」C:\ Users \ pc \ Desktop \ Alessio.xls「,0,true,5,」「,」「,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,」\ t「,false,false,0,true, 1,0);「 – Ale