简体   繁体   English

Excel DateTime 是一个字符串如何转换为实际日期时间 C#?

[英]Excel DateTime is a string how can I convert to real datetime C#?

The way my excel is formatted when programming in C# and I pull the cell with the supposed date I get a string "44165".在 C# 中编程时,我的 excel 被格式化的方式,我拉出带有假定日期的单元格,我得到一个字符串“44165”。 On Excel it shows as a date.在 Excel 上,它显示为日期。 I need to convert this to DateTime to do something else.我需要将其转换为 DateTime 以执行其他操作。 The string is equal to 2021-11-30 when I put it in excel and convert the column to datetime.当我将它放入 excel 并将列转换为日期时间时,该字符串等于 2021-11-30。 However when I try to convert to DateTime in C# I get the error "String was not recognized as a valid Datetime".但是,当我尝试在 C# 中转换为 DateTime 时,我收到错误“字符串未被识别为有效的日期时间”。

The value is read using:使用以下方式读取该值:

cell.CellValue.InnerXml

I have tried我努力了

Convert.ToDateTime,
DateTime.Parse,
DateTime.ParseExact 

None of which work and always result in "String was not recognized as a valid Datetime".这些都不起作用,并且总是导致“字符串未被识别为有效的日期时间”。 I cannot change the formatting of the excel I must find a solution within visual studio using C#.我无法更改 excel 的格式我必须在 Visual Studio 中使用 C# 找到解决方案。

I highly appreciate any help, thanks.我非常感谢任何帮助,谢谢。

double myOADate = 44165;
DateTime myDate = DateTime.FromOADate(myOADate);

Console.WriteLine(myDate.ToString());

The above returns以上返回

30/11/2020 00:00:00 2020 年 11 月 30 日 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM