MVC中将上一次导出的EXCEL文件导入验证并保存到数据库中

来源:互联网 发布:小马网络 编辑:程序博客网 时间:2024/06/04 20:09

1、网页中加入file元素,并添加表单

@using (Html.BeginForm("StationImport", "home", FormMethod.Post, new { enctype = "multipart/form-data" })){    <input type="submit" id="btnInExcel" onclick="" name="btnInExcel" value="导入" /><input id="files" type="file" name="files"  />}

2、在后台控制器加入

(1)对上传文件判断;正确保存到服务器中,错误返回

(2)将保存的文件进行转换成datatable,

(3)对datatable进行数据验证,并保存到数据库中

#region 批量导入excel        [HttpPost]        public ActionResult StationImport(HttpPostedFileBase filebase)        {            HttpPostedFileBase file = Request.Files["files"];            string FileName;            string savePath;            if (file == null || file.ContentLength <= 0)            {                return Content("<script>alert('文件不能为空');location='" + Url.Action("UserManager", "home") + "' </script>");            }            else            {                string filename = Path.GetFileName(file.FileName);                int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte                string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名                string NoFileName = System.IO.Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名                int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M                string FileType = ".xls,.xlsx";//定义上传文件的类型字符串                FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;                if (!FileType.Contains(fileEx))                {                    return Content("<script>alert('文件类型不对,只能导入xls和xlsx格式的文件');location='" + Url.Action("UserManager", "home") + "' </script>");                }                if (filesize >= Maxsize)                {                    return Content("<script>alert('上传文件超过4M,不能上传');location='" + Url.Action("UserManager", "home") + "' </script>");                }                string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/excel/";                Directory.CreateDirectory(path);                savePath = Path.Combine(path, FileName);                file.SaveAs(savePath);            }            //转换excel文件到datatable中            string strConn;            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";" + "Extended Properties=Excel 8.0";            OleDbConnection conn = new OleDbConnection(strConn);            conn.Open();            OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn);            DataSet myDataSet = new DataSet();            try            {                myCommand.Fill(myDataSet, "ExcelInfo");            }            catch (Exception ex)            {                ViewBag.error = ex.Message;                return View();            }            DataTable dt = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();                      //数据验证..将dt保存到数据库中返回result结果           string msg = result?"导入成功":"导入失败";            System.Threading.Thread.Sleep(2000);            return Content("<script>alert('" + msg + "');location='" + Url.Action("UserManager", "home") + "' </script>");        }        #endregion


0 0