C# NPOI的数据批量导入数据库

public ActionResult Upload(HttpPostedFileBase Namefile)
        {
            //判断文件是否存在
            if (Namefile == null)
            {
                return Content("没有文件");
            }
            //获取上传文件的名称,位置
            var fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + Namefile.FileName.Substring(Namefile.FileName.LastIndexOf('.'));
            //执行上传
            Namefile.SaveAs(Server.MapPath("/FileExcel/" + fileName));
            //获取文件路径,读取数据
            string url = Server.MapPath("/FileExcel/" + fileName);
            //dt为读取到的所有Excel里的数据
            DataTable dt = ExcelHelper.ExcelImport(url);

    //循环dt里的数据
            foreach (DataRow item in dt.Rows)
            {

      string str="insert添加语句"; 

            }

     //执行SQL语句
            SqlCommand sqlcommand = new SqlCommand(str.ToString(), GetConn());
            //执行数据导入
           int q=  sqlcommand.ExecuteNonQuery();

    if(q>0)

    {

      return Content("<script>alert('上传已完成,您上传成功!');location.href='/FuLi/FuLiIndex'</script>");

    }

    else

    {

      return Content("<script>alert('失败!');location.href='/FuLi/FuLiIndex'</script>");

    }
        } 

原文地址:https://www.cnblogs.com/liuyi-li/p/6340786.html