Cra*_*aig 2 c# spreadsheetgear
我正在使用电子表格设备读取现有的 Excel 文件并将数据导入到 SQL Server 数据库中。一切正常,直到我需要导入格式为日期的单元格 - “*3/14/2001”。我正在尝试将此字段导入到数据库中的日期时间字段中。不确定如何去做?
这是导入例程 -
fileInformation.DateReleased = worksheet.Cells["B20"].Text;
fileInformation.DateRequired = worksheet.Cells["B21"].Text;
public DateTime DateReleased { get; set; }
public DateTime DateRequired { get; set; }
Run Code Online (Sandbox Code Playgroud)
日期/时间作为表示日期/时间序列号的双精度值存储在 SpreadsheetGear(和 Excel)中。如果您愿意,您可以在此处阅读有关这些序列日期的更多信息。它们在单元格中显示为“日期”的事实只是应用于单元格的 IRange.NumberFormat 的函数(“m/d/yyyy”等)。您对 IRange.Text 的使用是将单元格的“格式化”值作为字符串返回。要从单元格获取实际的 .NET DateTime 值,您可以使用 IWorkbook。NumberToDateTime (...) 辅助方法。例子:
// Need the IWorkbook for which your "worksheet" object belongs to.
IWorkbook workbook = worksheet.Workbook;
// This code assumes there is *always* a date (double) value in this cell. You may want
// to do some additional error checking to ensure B20 actually has a number in it.
// Otherwise the cast will give you issues.
DateTime dateReleased = workbook.NumberToDateTime((double)worksheet.Cells["B20"].Value);
fileInformation.DateReleased = dateReleased;
DateTime dateRequired = workbook.NumberToDateTime((double)worksheet.Cells["B21"].Value);
fileInformation.DateRequired = dateRequired
Run Code Online (Sandbox Code Playgroud)