0
遇到此問題 'HttpRequest'不包含'Files'的定義並且沒有擴展方法可以找到接受類型'HttpRequest'的第一個參數的'Files'使用指令或程序集引用)MVC 4將Excel文件上傳到數據庫
不知道如何將日期轉換此訂單
user.StudentDob = workSheet.Cells [rowIterator,6] .Value.ToString()?;
任何人都可以幫忙嗎?試圖上傳Excel文件來閱讀。
這是我的控制器
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<Student>();
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 Student();
user.StudentId = workSheet.Cells[rowIterator, 1].Value.ToString().FirstOrDefault();
user.StudentNric = workSheet.Cells[rowIterator, 2].Value.ToString();
user.StudentName = workSheet.Cells[rowIterator, 3].Value.ToString();
user.StudentContact = workSheet.Cells[rowIterator, 4].Value.ToString().FirstOrDefault();
user.StudentEmail = workSheet.Cells[rowIterator, 5].Value.ToString();
user.StudentDob = workSheet.Cells[rowIterator, 6].Value.ToString();
user.FirstLogin = workSheet.Cells[rowIterator, 7].Value.ToString();
usersList.Add(user);
}
}
}
}
return View("Index");
}
這是我的模型
public partial class Student
{
public int StudentId { get; set; }
public string StudentNric { get; set; }
public string StudentName { get; set; }
public int StudentContact { get; set; }
public string StudentEmail { get; set; }
public DateTime StudentDob { get; set; }
public string FirstLogin { get; set; }
}
怎麼樣的錯誤「的HttpRequest」不包含一個定義爲「文件」,並沒有擴展方法「的文件'可以找到接受'HttpRequest'類型的第一個參數。 –