Spreadsheetgear:转换为date时间时出错

我试图分析Excel工作表中的单元格值,并使用下面的代码将其转换为date时间。但我得到一个错误:

'arguments.CurrentWorksheet.Workbook.NumberToDateTime(inpDate)'抛出一个'System.ArgumentOutOfRangeException'types的exception。base:{“指定的参数超出了有效值范围。\ r \ n参数名称:无效的date – 时间序列号。 “} ActualValue:null消息:”指定的参数超出了有效值的范围。\ r \ n参数名称:无效的date – 时间序列号。

码:

double inpDate = arguments.GetNumber(2); startDate = arguments.CurrentWorksheet.Workbook.NumberToDateTime(inpDate); 

其中工作表中的内容是20170113。

你能build议吗?

谢谢。

您的inpDate不是有效的“Exceldate”。 date/时间存储在SpreadsheetGear和Excel中,作为代表date/时间序列号double 精度型 。 整数代表天数,其中值1是1900年1月1日,数字的小数部分代表时间(即0.5是中午)。 因此,例如,值为42793.0表示2017年2月27日(上午12:00)。 您可以在以下信息页面上阅读有关Exceldate的更多信息: http : //www.cpearson.com/excel/datetime.htm

IWorkbook。 NumberToDateTime (…)需要这种序列date,但是您input的'20170113'(我假设应该代表2017年1月13日)正在被解释为未来的date方式,就像接近年份的某个地方57,160(自1900年1月1日起20,170,113天),这在Excel / SpreadsheetGear中是一个有效的date。

对于您的具体情况,使用DateTime似乎更合适。 ParseExact (…),如:

 DateTime dateTime = DateTime.ParseExact("20170113", "yyyyMMdd", CultureInfo.CurrentCulture);