Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从excel中获取值时,日期已更改为数字_C#_Excel_Datetime_Dataset - Fatal编程技术网

C# 从excel中获取值时,日期已更改为数字

C# 从excel中获取值时,日期已更改为数字,c#,excel,datetime,dataset,C#,Excel,Datetime,Dataset,我有一个excel文件,其中包含许多字段。我使用数据集存储从这个excel文件中获取的数据。在excel工作表中,我有一个设置为正确格式的日期列。但检索时,格式将更改为数据集中的数字 我不知道这个问题的原因 Excel值:2015年12月1日,在数据集中更改为42016 任何关于这个问题的建议都将不胜感激 我的代码是 private static DataSet GetExcelDataAsDataSet(string path) { return

我有一个excel文件,其中包含许多字段。我使用数据集存储从这个excel文件中获取的数据。在excel工作表中,我有一个设置为正确格式的日期列。但检索时,格式将更改为数据集中的数字

我不知道这个问题的原因

Excel值:2015年12月1日,在数据集中更改为42016

任何关于这个问题的建议都将不胜感激

我的代码是

  private static DataSet GetExcelDataAsDataSet(string path)
        {

            return GetExcelDataReader(path).AsDataSet();
        }

        private static IExcelDataReader GetExcelDataReader(string path)
        {
            FileStream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read);


            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            var data = excelReader.ResultsCount;
            DataSet result = excelReader.AsDataSet();


            excelReader.IsFirstRowAsColumnNames = true;

            return excelReader;
        }
如中所述,它与使用一样简单:

public static DateTime FromExcelSerialDate(int SerialDate)
{
    if (SerialDate > 59) SerialDate -= 1; //Excel/Lotus 2/29/1900 bug   
    return new DateTime(1899, 12, 31).AddDays(SerialDate);
}
您得到的数字是自1900年1月1日以来经过的天数,因此该函数就是您的答案:)


您还可以使用
DateTime dt=DateTime.FromOADate(NUMBER)我认为从中提取会更好。

能否提供将excel中的数据填充到数据集中的代码段?原因是您检索的值是VBA的数字表示形式,而不是日期值。但如果没有您的代码,我们无法建议如何改变这种行为。