首页 > 代码库 > C# ----Excel读取数据之数据引擎方法

C# ----Excel读取数据之数据引擎方法

读取Excel表数据,可以使用连接Excel数据引擎的方法

代码如下:

        /// <summary>        /// excel转换为DataTable        /// </summary>        /// <param name="filename">excel文件名称</param>        /// <param name="sheet">excel表单名称</param>        /// <returns></returns>        private DataTable ExcelToDataTable(string filename,string sheet)        {            DataTable dt = new DataTable();            //判断文件是否存在            if (!File.Exists(filename))            {                DevExpress.XtraEditors.XtraMessageBox.Show(filename + "file don‘t exist!");            }            else            {                //文件后缀名获取                string extension = Path.GetExtension(filename);                //excel数据引擎连接字符串                string strConn = "";                if (extension == ".xls")                {                    strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";                }                else if (extension == ".xlsx")                {                    strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\"";                }                ArrayList sheetList = new ArrayList();                //获取表单列表                sheetList = ExcelTables(filename,strConn);                //如果数据表单名不存在,取EXCEL中第一个数据表单                if (sheetList.IndexOf(sheet) < 0)                {                    sheet = sheetList[0].ToString().Trim();                }                //建立excel数据引擎连接                System.Data.OleDb.OleDbConnection dbconn = new System.Data.OleDb.OleDbConnection(strConn);                //查询表命令                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("select * from [" + sheet + "$]", dbconn);                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(cmd);                try                {                    if (dbconn.State == ConnectionState.Closed)                    {                        dbconn.Open();                    }                    adapter.Fill(dt);                }                catch (Exception ex)                {                    throw ex;                }                finally                {                    if (dbconn.State == ConnectionState.Open)                    {                        dbconn.Close();                    }                }            }            return dt;        }        /// <summary>        /// 获取Excel表单列表        /// </summary>        /// <param name="filename"></param>        /// <returns></returns>        private ArrayList ExcelTables(string filename,string strConn)        {            DataTable dt = new DataTable();            ArrayList sheetList = new ArrayList();            //判断文件名是否存在以及连接字符串是否正确            if (File.Exists(filename)&&!string.IsNullOrEmpty(strConn))            {                using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))                {                    try                    {                        conn.Open();                        //返回excel数据引擎的架构信息                        dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });                    }                    catch (Exception ex)                    {                        throw ex;                    }                    int sheetCount = dt.Rows.Count;                    //获取excel表单列表                    for (int i = 0; i < sheetCount; i++)                    {                        string tablename = dt.Rows[i][2].ToString().Trim().TrimEnd($);                        if (sheetList.IndexOf(tablename) < 0)                        {                            sheetList.Add(tablename);                        }                    }                }            }            return sheetList;        }
View Code