上传 Excel 列数据为 dateTime 但 C# 无法计算出 NPOI 的文件

Upload Excel file whose column data is dateTime but C# could not figure out NPOI

我的 Excel 有像

这样的列数据

当我使用下面的代码在 asp.net api 中获取数据时,我得到的是 01-1月-2021 而不是 2021-01-01 的数据,然后我在行 entity.startTime = Convert.ToDateTime(row[0].ToString().Trim());

 for (int j = 0; j < HttpContext.Current.Request.Files.Count; j++)
        {
            string fileName = HttpContext.Current.Request.Files[j].FileName;
            if (fileName.Contains(".xlsx") || fileName.Contains(".xls"))
            {
                dataset = ExcelHelper.ExcelStreamToDataSet(HttpContext.Current.Request.Files[j].InputStream, HttpContext.Current.Request.Files[j].FileName);
              
                bool isFileFind = false;
                List<ExcelRowErrorInfoAssetInfo> fileErrorAllList = new List<ExcelRowErrorInfoAssetInfo>();
               
                for (int i = 0; i < dataset.Tables.Count; i++)
                {
                    DataTable table = dataset.Tables[i];
                    string sheetName = table.TableName;
                    #region 
                    for (int r = 0; r < table.Rows.Count; r++)
                    {
                        DataRow row = table.Rows[r];
                        int cnt = row.ItemArray.ToList().Where(a => string.IsNullOrEmpty(a.ToString())).Count();
                        if (cnt < row.ItemArray.Count())
                        {
                            if (sheetName == "基本信息")
                            {
                                AssetsInfoMap entity = new AssetsInfoMap();
                                //startTime  is type of DateTime
                                entity.startTime = Convert.ToDateTime(row[0].ToString().Trim());
                                entity.endTime = Convert.ToDateTime(row[1].ToString().Trim());
                                
                            }
                        }
                    }
                    #endregion
                }

public static DataSet ExcelStreamToDataSet(Stream stream, string fileName)
    {
        DataSet dataset = new DataSet();
        int startRow = 0;
        try
        {
            IWorkbook workbook = null;
            if (fileName.IndexOf(".xlsx") > 0) 
                workbook = new XSSFWorkbook(stream);
            else if (fileName.IndexOf(".xls") > 0) 
                workbook = new HSSFWorkbook(stream);

            int sheetCount=workbook.NumberOfSheets;
            for (int m = 0; m < sheetCount-1; m++)
            {
                DataTable data = new DataTable();
                ISheet sheet = workbook.GetSheetAt(m);
                data.TableName= sheet.SheetName;
                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(0);
                    int cellCount = firstRow.LastCellNum; 

                    for (int k = firstRow.FirstCellNum; k < cellCount; ++k)
                    {
                        ICell cell = firstRow.GetCell(k);
                        if (cell != null)
                        {
                            string cellValue = cell.StringCellValue;
                            if (cellValue != null)
                            {
                                DataColumn column = new DataColumn(cellValue);
                                data.Columns.Add(column);
                            }
                        }
                    }
                    startRow = sheet.FirstRowNum + 1;

                  
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue; 
                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            if (row.GetCell(j) != null) 
                                dataRow[j] = row.GetCell(j).ToString();
                        }
                        data.Rows.Add(dataRow);
                    }
                }
                dataset.Tables.Add(data);
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
        return dataset;
    }
}

那么如何获取正确的数据并将其分配给我的模型,您根据我上面的代码有什么想法吗?

ICell 定义了一个 属性 .DateCellValue,因此在映射日期值时应使用它:

ICell cell = row.GetCell(j);
if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell))
{
    dataRow[j] = cell.DateCellValue;
}

这会将您的值存储为 DateTime 而不是 string。我们现在需要取消您的 DateTime->string->DateTime 转换:

entity.startTime = (DateTime)row[0];