如何将数据从Excel导入到MVC3中的SQL Server Express 2008

Sam*_*m M 3 asp.net-mvc sql-server-2008-express asp.net-mvc-3

我正在使用带有C#代码的MVC3.我的SQL Server Express中有一个包含一些列的表,我有一个Excel表,它具有相同数量的列,具有相同的数据类型和名称.

我的要求是我想在我的MVC3应用程序中浏览该Excel文件,用户可以在其中选择文件.RegistrationNo我的数据库表和Excel工作表中都有一列.在导入数据库表格中的数据之前,RegNo应将Excel 表格中的数据与数据库表格中的数据进行比较,RegNo如果RegNo已经存在,RegNO那么如果RegNo表格中不存在该行,则应该不进行插入.为此RegNo应该插入.

下面是我尝试过的代码,但是我遇到了很多问题.

[HttpPost]
public ActionResult AdmissionUpload()
{
    string filePath = null;
    foreach (string inputTagName in Request.Files)
    {
       HttpPostedFileBase Infile = Request.Files[inputTagName];      
       if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".xls" || Path.GetExtension(Infile.FileName) == ".xlsx" || Path.GetExtension(Infile.FileName) == ".xlsm"))
       {
          filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                        Path.GetFileName(Infile.FileName));
          if (System.IO.File.Exists(filePath))
          {
             System.IO.File.Delete(filePath);
          }
          Infile.SaveAs(filePath);
          //Infile.SaveAs(filePath); 
       }

       if (filePath != null)
       {
          System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath.ToString() + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
          oconn.Open();

          try
          {
             if (oconn.State == System.Data.ConnectionState.Closed)
                oconn.Open();
          }
          catch (Exception ex)
          {
             // MessageBox.Show(ex.Message);
          }

          dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
          OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);
          OleDbDataReader odr = ocmd.ExecuteReader();

          if (odr.HasRows)
          {
             while (odr.Read())
             {
                 if (odr[0].ToString().Trim() != "")
                 {
                    if (CheckDepartment(odr[0].ToString().Trim()) == false)
                    {
                       var model = new DepartmentMaster();
                       model.DepartmentName = odr[1].ToString().Trim();
                       db.DepartmentMasters.AddObject(model);
                       db.SaveChanges();
                       FLAG = true;
                    }
                 }
              }
          }
       }
   }
   return View();
}   
Run Code Online (Sandbox Code Playgroud)

这里CheckRegNo检查是否RegNo存在.

Sam*_*m M 5

dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);
OleDbDataReader odr = ocmd.ExecuteReader();
if (odr.HasRows)
{
     while (odr.Read())
 {
     var model = new Student();
     model.Col1=Convert.ToInt32(odr[0]);
     model.Col2 = odr[1].ToString().Trim();
     model.col3 = odr[2].ToString().Trim();
     model.col4 = odr[3].ToString().Trim();
    db.MyTable.AddObject(model);                            
 }
}
Run Code Online (Sandbox Code Playgroud)

这就是我如何阅读Excel并从Excel保存数据.