日期:2014-05-18 浏览次数:20526 次
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =" + path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; OleDbConnection cnnxls = new OleDbConnection(mystring); string strExcel = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter myDa = new OleDbDataAdapter(strExcel, cnnxls); DataSet myDs = new DataSet(); myDa.Fill(myDs); //该行报错
/// <summary> /// 获取Excel文件中的数据 /// </summary> /// <param name="filepath">Excel文件的绝对路径</param> /// <param name="sheetname">Excel工作薄</param> /// <returns></returns> public static DataSet ExcelDataSource(string filepath, string sheetname) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con = new OleDbConnection(strConn); OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + sheetname + "$]", con); DataSet ds = new DataSet(); oda.Fill(ds); return ds; }
------解决方案--------------------
DataTable Excel_UserInfo = new DataTable(); string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\""; string strExcel = "select * from [sheet1$]"; using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn)) { DataSet ds = new DataSet(); adaptor.Fill(ds); Excel_UserInfo = ds.Tables[0]; }