C#无法从电子表格中读取Excel日期 [英] C# not reading excel date from spreadsheet
问题描述
我使用Microsoft.Office.Interop.Excel上传了一个Excel电子表格.当我尝试读取带有日期值的单元格以将其插入到我的数据集中时,无法将其识别为日期,而是随机数吗?这是我指代单元格的方式:
I uploaded an excel spreadsheet using Microsoft.Office.Interop.Excel. When I try to read cells with a date value in order to insert it into my data set, its not being recognized as a date and it comes up as a random number? this is the way that I refer to the cell:
Excel.Range startDate = objsheet.get_Range("C1:C" + lastUsedRow, System.Type.Missing);
double dbl = Convert.ToDouble(startDate);
DateTime conv = DateTime.FromOADate(dbl);
(row[3] = ((Microsoft.Office.Interop.Excel.Range)objsheet.Cells[rowIndex, 4]).Value2;)
推荐答案
来自 https://stackoverflow.com/a/4538367/1397117
您需要将日期格式从OLE Automation转换为.net通过使用DateTime.FromOADate进行格式化.
You need to convert the date format from OLE Automation to the .net format by using DateTime.FromOADate.
double d = double.Parse(b);
DateTime conv = DateTime.FromOADate(d);
我在下面的答案中建议使用 .Value
而不是 .Value2
.
And I echo suggestions below that answer to use .Value
instead of .Value2
.
这篇关于C#无法从电子表格中读取Excel日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!