ADO.NET 读取Excel文件,并作数据源

//若是在Web.config中配置数据源,如下
<add key="ExcelConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source='miroExcel/Info.xls';Extended Properties='Excel 8.0;HDR=yes;IMEX=2'"/>
 
        /// <summary>
        /// 读取Excel文件  传入绝对路径  查询条件
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetBoolExcelDAL(string cardId, string excelPath)
        {
            try
            {
                string strCon = "Provider=Microsoft.Ace.OleDb.12.0; Data Source ='" + excelPath + "';Extended Properties='Excel 12.0;HDR=yes;IMEX=2'";
                OleDbConnection Conn = new OleDbConnection(strCon);
                string strsql = string.Format("select * from [Sheet1$] where CardId='{0}'", cardId);
                Conn.Open();
                OleDbCommand oleCommand = new OleDbCommand(strsql, Conn);
                OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                DataSet ds = new DataSet();
                oleAdapter.Fill(ds, "ExcelData");
                if (ds != null)
                {
                    return ds;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }

 

你可能感兴趣的:(Excel)