C#导入Excel|读取Excel方法

时间:2021-03-07 04:46:54

OleDbConnection读取

/// <summary>       /// 返回Excel数据源       /// </summary>       /// <param name="filename">文件路径</param>       /// <param name="TSql">TSql</param>       /// <returns>DataSet</returns>       public static DataSet ExcelToDataSet(string filename, string TSql)       {           DataSet ds;           string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + filename;           OleDbConnection myConn = new OleDbConnection(strCon);           string strCom = TSql;           myConn.Open();           OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);           ds = new DataSet();           myCommand.Fill(ds);           myConn.Close();           return ds;       }       写sql语句           string TSql = "SELECT  * FROM [sheel1$]";        //读取数据        DataTable table = ExcelToDataSet(fullPath, TSql).Tables[0];