Quantcast
Channel: Excel Data Reader - Read Excel files in .NET
Viewing all articles
Browse latest Browse all 448

Commented Unassigned: Date incorrectly read as Double [11860]

$
0
0
Hi

when trying to read attached file, the reader is incorrectly reading the first column's values (from row 4 down) as double. Instead it should be either string or datetime.

Any fix/workaround for this? We really like this tool but can't use without fixing this.

Thanks,
Stevo
Comments: ** Comment from web user: jaklithn **

Thanks for quick answer!
Excel file is enclosed. Second column holds dates having the value 2014-01-16 16:00:00
I use the following code:

public static void GetValues(TableItem table, string path, string sheetName, bool firstRowHoldsColumnNames = true)
{
using (var stream = File.Open(path, FileMode.Open, FileAccess.Read))
{
using (var excelReader = GetExcelDataReader(path, stream))
{
excelReader.IsFirstRowAsColumnNames = firstRowHoldsColumnNames;
var dataset = excelReader.AsDataSet();
foreach (DataRow row in dataset.Tables[sheetName].Rows)
{
var rowItem = new RowItem();
foreach (var value in row.ItemArray)
{
rowItem.Values.Add(value);
}
table.Rows.Add(rowItem);
}
}
}
}

private static IExcelDataReader GetExcelDataReader(string path, Stream stream)
{
var extension = GetExtension(path);
switch (extension)
{
case "xls":
return ExcelReaderFactory.CreateBinaryReader(stream);
case "xlsx":
return ExcelReaderFactory.CreateOpenXmlReader(stream);
default:
throw new Exception(string.Format("Extension='{0}' is not handled", extension));
}
}

private static string GetExtension(string path)
{
var extension = Path.GetExtension(path);
return extension == null ? null : extension.ToLower().Substring(1);
}


Viewing all articles
Browse latest Browse all 448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>