Пример #1
0
        public ActionResult UploadExcel(HttpPostedFileBase file)
        {
            if (file == null)
            {
                return(Content("没有文件!", "text/plain"));
            }
            var rootPath = Request.MapPath("~/Upload/Excel/User");

            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath); //不存在就创建文件夹
            }
            var fileName = Path.Combine(rootPath, Path.GetFileName(file.FileName));

            try
            {
                file.SaveAs(fileName);
                DataTable dtData = ExcelHelper.Import(fileName, 1);
                var       list   = new NPOIExcel().ConvertTo <UserExcelInPut>(dtData, true).ToList();
                userApp.ExcelImport(list);

                return(Content("上传成功!", "text/plain"));
            }
            catch (Exception ex)
            {
                return(Content("上传异常 !", "text/plain"));
            }
        }