批量导入excel 狼

 原理:excel--就是个数据库--用sql语句将其读入到dataset中,然后再处理得得到的dataset,在保持到数据库中。

/// <summary>
        /// 读取文件进行导入
        /// </summary>
        /// <param name="fileName">必须是全路径</param>
        private DataSet ImportToExcel(string fileName)
        {
            DataSet ds = new DataSet();
            try
            {
                OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + fileName);
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }
                else
                {

                    TrainexamCommon.ExtMessageBox.Show("成绩导入错误提示:", "您选择的EXCEL文件正在被使用!请关闭后重试!", "OK", "ERROR");
                    return null;
                }
                OleDbCommand cmd = new OleDbCommand(@"select * from [Sheet1$]", con);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }

原文地址:https://www.cnblogs.com/gowhy/p/2079792.html