读取Excel

  public static class ExcelHelper
    {
        #region 查询Excel
        /// <summary>
        /// 将Excel文件导出至DataTable(第一行作为表头)
        /// </summary>
        /// <param name="ExcelFilePath">Excel文件路径</param>
        /// <param name="TableName">数据表名,如果数据表名错误,默认为第一个数据表名</param>
        public static DataTable GetDataFromExcel(string ExcelFilePath, string TableName = null)
        {
            if (!File.Exists(ExcelFilePath))
            {
                throw new Exception("Excel文件不存在!");
            }

            //如果数据表名不存在,则数据表名为Excel文件的第一个数据表
            List<string> TableList = GetExcelTables(ExcelFilePath);
            if (string.IsNullOrEmpty(TableName) || !TableList.Contains(TableName))
            {
                TableName = TableList[0].ToString().Trim();
            }

            DataTable table = new DataTable();
            OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFilePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1' ");
            OleDbCommand cmd = new OleDbCommand("select * from [" + TableName + "$]", dbcon);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

            try
            {
                if (dbcon.State == ConnectionState.Closed)
                {
                    dbcon.Open();
                }
                adapter.Fill(table);
            }
            catch (Exception exp)
            {
                // throw exp;
            }
            finally
            {
                if (dbcon.State == ConnectionState.Open)
                {
                    dbcon.Close();
                }
            }

            return table;
        }

        /// <summary>
        /// 获取Excel文件数据表列表
        /// </summary>
        public static List<string> GetExcelTables(string ExcelFileName)
        {
            DataTable dt = new DataTable();
            List<string> TablesList = new List<string>();
            if (File.Exists(ExcelFileName))
            {
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + ExcelFileName))
                {
                    try
                    {
                        conn.Open();
                        dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                    }
                    catch (Exception exp)
                    {
                        throw exp;
                    }

                    //获取数据表个数
                    int tablecount = dt.Rows.Count;
                    for (int i = 0; i < tablecount; i++)
                    {
                        string tablename = dt.Rows[i][2].ToString().Trim().TrimEnd('$');
                        if (TablesList.IndexOf(tablename) < 0)
                        {
                            TablesList.Add(tablename);
                        }
                    }
                }
            }
            return TablesList;
        }
        #endregion
    }
ExcelHelper
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.InitialDirectory = AppPath;
            ofd.ShowDialog();
            string fileUrl = ofd.FileName;
            if (string.IsNullOrEmpty(fileUrl))
            {
                return;
            }
            try
            {
                DataTable exclTd = new DataTable();
                exclTd = ExcelHelper.GetDataFromExcel(fileUrl, "sheet1");
            }
            catch (Exception ex)
            {

            }
        }
调用
原文地址:https://www.cnblogs.com/junhuang/p/6438296.html