这是vs2010中 导入的一个方法,每次执行到ExcelConn.Open();就自动退出了。
private DataSet doImport(string strFileName) { if (strFileName == "") return null; string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "DataSource=" + strFileName + ";" + "Extended Properties=Excel 8.0;"; string strGetExcel = "SELECT * FROM [Sheet1$]";
OleDbConnection ExcelConn = new OleDbConnection(strConn); ExcelConn.Open();
OleDbDataAdapter ExcelDA = new OleDbDataAdapter(strGetExcel, strConn); DataSet ExcelDS = new DataSet(); try { ExcelDA.Fill(ExcelDS, "Test"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } ExcelConn.Close(); return ExcelDS; }
我在ExcelConn.Open();那设置了断点,结果报下面的错误:
紧接着,在逐句执行就调用下面的方法清理资源:
到底是为什么也搞不清楚啊。。
这是我的一段导入excel文件代码:
OpenFileDialog openExcelFile = new OpenFileDialog();我估计是你的strConn 写的不对。你试一下我的看看,实现的方法和你的差不多,只是strConn不一样。