-1

我正在使用以下代码导入 excel 并读取文件 https://www.c-sharpcorner.com/blogs/upload-and-read-excel-file-in-mvc1

在从日期列获取数据时,它会自动转换为字符串。在这里,此列在 excel 中被格式化为日期,所以如果有日期 '9/9/2019' 我得到 43717

这是我的最小代码:

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

有关更多代码,您可以访问上面的链接。

4

1 回答 1

0

您必须将字符串转换为 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);  
} 
于 2019-12-26T09:13:40.610 回答