public DataSet ReadExcelM(string sExcelFile) { try { DataTable ExcelTable; DataSet ds = new DataSet(); //Excel的连接 OleDbConnection objConn = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", sExcelFile)); objConn.Open(); DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); //获取 Excel 的表名,默认值是sheet1 string tableName = schemaTable.Rows[0][2].ToString().Trim(); string strSql = "select * from [" + tableName + "]"; OleDbCommand objCmd = new OleDbCommand(strSql, objConn); OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); myData.Fill(ds, tableName);//填充数据 return ds; } catch (Exception ex ) { throw ex ; } }
需要在服务器上安装 AccessDatabaseEngine.exe 或者 AccessDatabaseEngine_X64.exe,windows版本不同装的也不一样
下载地址 http://www.microsoft.com/zh-cn/download/details.aspx?id=13255