使用EPPLUS上传Excel文件时的MVC自定义验证

时间:2019-01-07 12:14:09

标签: c# asp.net-mvc

如果名字和姓氏为空,如何验证它们,请参考下面的链接以获取完整代码

https://social.technet.microsoft.com/wiki/contents/articles/31790.asp-net-mvc-upload-read-excel-file.aspx

    public ActionResult Upload(FormCollection formCollection)
    {
        if (Request != null)
        {
            HttpPostedFileBase file = Request.Files["UploadedFile"];
            if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
            {
                string fileName = file.FileName;
                string fileContentType = file.ContentType;
                byte[] fileBytes = new byte[file.ContentLength];
                var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                var usersList = new List<Users>();
                using (var package = new ExcelPackage(file.InputStream))
                {
                    var currentSheet = package.Workbook.Worksheets;
                    var workSheet = currentSheet.First();
                    var noOfCol = workSheet.Dimension.End.Column;
                    var noOfRow = workSheet.Dimension.End.Row;

                    for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                    {
                        var user = new Users();
                        user.FirstName = workSheet.Cells[rowIterator, 1].Value.ToString();
                        user.LastName = workSheet.Cells[rowIterator, 2].Value.ToString();
                        usersList.Add(user);
                    }
                }
            }
        }
        return View("Index");
    }

0 个答案:

没有答案