Forum Discussion
ExcelDataReader and Date Field
I am looping through the Excel document saved withing my project (wwwroot) and I cannot get the date to work. I have searched and found a reference to call GetDateTime as opposed to GetValue and also attempted to use ToString() and ToShortDateString();
Cannot implicitly convert type 'string' to 'System.DateOnly'
What am I missing?
using (var stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
do
{
while (reader.Read())
{
BirthModel birth = new BirthModel();
birth.LastName = reader.GetValue(1).ToString();
birth.FirstName = reader.GetValue(2).ToString();
birth.MiddleName = reader.GetValue(3).ToString();
birth.BirthDate = reader.GetDateTime(4).ToShortDateString();
birth.County = reader.GetValue(5).ToString();
birth.Maiden_FirstName = reader.GetValue(6).ToString();
birth.Maiden_MiddleName = reader.GetValue(7).ToString();
birth.Maiden_LastName = reader.GetValue(8).ToString();
}
} while (reader.NextResult());
1 Reply
- wuchen9527Copper Contributor
是否是你的“reader.GetDateTime(4)”返回的是null,或者是他里面执行就直接报错了