-1

I'm using following code to import excel and read the file https://www.c-sharpcorner.com/blogs/upload-and-read-excel-file-in-mvc1

while getting data from date column it automatically converted to string.here this column is formatted as date in excel so if there is date '9/9/2019' im getting 43717

here is my minimal code:

for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)   
{
    var ex = new ExcelData();
    ex.CostCode = workSheet.Cells[rowIterator, 1].Value.ToString();
    ex.EmployeeNumber = workSheet.Cells[rowIterator, 2].Value.ToString();
    ex.Class = workSheet.Cells[rowIterator, 3].Value.ToString();
    ex.Date = workSheet.Cells[rowIterator, 4].Value.ToString();
    ex.Hours = workSheet.Cells[rowIterator, 5].Value.ToString();
    ex.PayType = workSheet.Cells[rowIterator, 6].Value.ToString();
    dataList.Add(ex);  
} 

for more code you can visit the above link.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
devSS
  • 39
  • 2
  • 7

1 Answers1

0

You must make converting string to DateTime...

ex.Date = workSheet.Cells[rowIterator, 4].Value.ToString("yyyy-MM-dd hh:ss");

for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)   
{
    var ex = new ExcelData();
    ex.CostCode = workSheet.Cells[rowIterator, 1].Value.ToString();
    ex.EmployeeNumber = workSheet.Cells[rowIterator, 2].Value.ToString();
    ex.Class = workSheet.Cells[rowIterator, 3].Value.ToString();
    ex.Date = workSheet.Cells[rowIterator, 4].Value.ToString();
    ex.Hours = workSheet.Cells[rowIterator, 5].Value.ToString();
    ex.PayType = workSheet.Cells[rowIterator, 6].Value.ToString();
    dataList.Add(ex);  
} 
Jesse
  • 168
  • 1
  • 7