日期:2014-05-17 浏览次数:20430 次
/// <summary> /// 读取用户指定WorkSheet的数据,作为DataTable输出 /// </summary> /// <param name="excelFilePath">被读取的Excel文档路径</param> /// <param name="HeadRowAsColumnCaption">是否将Excel第一行作为DataTable表头输出</param> /// <param name="workSheetName">被读取的WorkSheet名称</param> /// <returns></returns> public static System.Data.DataTable ReadExcelToDataTable(string excelFilePath, bool HeadRowAsColumnCaption, string workSheetName) { System.Data.DataTable dataTable = new System.Data.DataTable(); System.Data.OleDb.OleDbConnection conn = GetConnection(excelFilePath, HeadRowAsColumnCaption); if (conn != null) { conn.Open(); string strSql = "select * from [" + workSheetName + "$]"; System.Data.OleDb.OleDbDataAdapter sda = new System.Data.OleDb.OleDbDataAdapter(strSql, conn); dataTable.TableName = workSheetName; sda.Fill(dataTable); conn.Close(); } return dataTable; }