导入

来源:互联网 发布:捷豹系统彩票源码 编辑:程序博客网 时间:2024/04/28 22:15

public ActionResult Admin(HttpPostedFileBase filebase)

        {

            HttpPostedFileBase file = Request.Files["files"];

            string FileName;

            string savePath;

            if (file == null || file.ContentLength <= 0)

            {

                ViewBag.error = "文件不能为空";

                return View();

            }

            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))

                {

                    ViewBag.error = "文件类型不对,只能导入xlsxlsx格式的文件";

                    return View();

                }

                if (filesize >= Maxsize)

                {

                    ViewBag.error = "上传文件超过4M,不能上传";

                    return View();

                }

                string path = AppDomain.CurrentDomain.BaseDirectory + "Excel/";

                savePath = Path.Combine(path, FileName);

                file.SaveAs(savePath);

            }

 

            //string result = string.Empty;

            string strConn;

            strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + savePath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";

            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 table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();

 

            //引用事务机制,出错时,事物回滚

            using (TransactionScope transaction = new TransactionScope())

            {

                for (int i = 0; i < table.Rows.Count; i++)

                {

                    ////获取地区名称

                    //string _areaName = table.Rows[i][0].ToString();

                    ////判断地区是否存在

                    //if (!_areaRepository.CheckAreaExist(_areaName))

                    //{

                    // ViewBag.error = "导入的文件中:" + _areaName + "地区不存在,请先添加该地区";

                    // return View();

                    //}

                    //else

                    //{

                    // Station station = new Station();

                    // station.AreaID = _areaRepository.GetIdByAreaName(_areaName).AreaID;

                    // station.StationName = table.Rows[i][1].ToString();

                    // station.TerminaAddress = table.Rows[i][2].ToString();

                    // station.CapacityGrade = table.Rows[i][3].ToString();

                    // station.OilEngineCapacity = decimal.Parse(table.Rows[i][4].ToString());

                    // _stationRepository.AddStation(station);

                    //}

 

 

                    //循环保存

                    LeBlog.Service.UserService server = new UserService();

                    UserInfo uif = new UserInfo();

                    uif.UserName = table.Rows[i][0].ToString();

                    uif.RealName = table.Rows[i][1].ToString();

                    uif.Password = table.Rows[i][2].ToString();

                    uif.Autograph = table.Rows[i][3].ToString();

                    if (server.Regist(uif))

                    {

                        transaction.Complete();

                    }

 

                }

 

            }

            ViewBag.error = "导入成功";

            System.Threading.Thread.Sleep(2000);

            return Content("<script>alert('数据导入成功!');location.href='/Home/Index'</script>");

        }

    }

 

0 0