使用 Spreadsheetgear 导入日期时间格式的单元格
Import DateTime formated cell using Spreadsheetgear
我正在使用 spreadsheetgear 读取现有 Excel 文件并将数据导入 SQL 服务器数据库。一切正常,直到我需要导入格式为日期的单元格 - “*3/14/2001”。我正在尝试将此字段导入数据库中的 DateTime 字段。不确定该怎么做?
这是导入例程-
fileInformation.DateReleased = worksheet.Cells["B20"].Text;
fileInformation.DateRequired = worksheet.Cells["B21"].Text;
public DateTime DateReleased { get; set; }
public DateTime DateRequired { get; set; }
Dates/times 作为代表 date/time 序列号的双精度值存储在 SpreadsheetGear(和 Excel)中。您可以阅读有关这些序列日期 here if you want. The fact that they show up as a "date" in the cell is simply a function of the IRange.NumberFormat applied to the cell ("m/d/yyyy" etc). Your use of IRange.Text is returning the "formatted" value of the cell--as a string. To get an actual .NET DateTime value from a cell, you can use the 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
我正在使用 spreadsheetgear 读取现有 Excel 文件并将数据导入 SQL 服务器数据库。一切正常,直到我需要导入格式为日期的单元格 - “*3/14/2001”。我正在尝试将此字段导入数据库中的 DateTime 字段。不确定该怎么做?
这是导入例程-
fileInformation.DateReleased = worksheet.Cells["B20"].Text;
fileInformation.DateRequired = worksheet.Cells["B21"].Text;
public DateTime DateReleased { get; set; }
public DateTime DateRequired { get; set; }
Dates/times 作为代表 date/time 序列号的双精度值存储在 SpreadsheetGear(和 Excel)中。您可以阅读有关这些序列日期 here if you want. The fact that they show up as a "date" in the cell is simply a function of the IRange.NumberFormat applied to the cell ("m/d/yyyy" etc). Your use of IRange.Text is returning the "formatted" value of the cell--as a string. To get an actual .NET DateTime value from a cell, you can use the 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