简体   繁体   中英

How can I resolve a System.FormatException error on DateTime parsing when using ExcelDataReader.CreateReader()?

I receive an .xlsx file from a supplier and it contains a column for duration in HH:mm:ss format, which when I open the file in Excel appears as a date but should really just be a string.

I would like to read this .xslx file into my c# application using ExcelDataReader without having to manually modify or delete the column before running my application.

FileStream fs = File.Open(extractedFilePath, FileMode.Open, FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(fs);
//I've also tried using ExcelReaderFactory.CreateOpenXmlReader(fs) and get the same error

However when I attempt to create a reader as above, I get the following error:

System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.DateTime.ParseExact(String s, String format, IFormatProvider provider, DateTimeStyles style)
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.ConvertCellValue(String rawValue, String aT, String aS)
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.ReadCell(XmlReader xmlReader, Int32 nextColumnIndex)
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.ReadRow(XmlReader xmlReader, Int32 nextRowIndex)
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.<ReadSheetData>d__73.MoveNext()
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.<ReadWorksheetStream>d__71.MoveNext()
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.<ReadWorksheetStream>d__70.MoveNext()
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet.ReadWorksheetGlobals()
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorksheet..ctor(ZipWorker document, XlsxWorkbook workbook, XlsxBoundSheet refSheet)
at ExcelDataReader.Core.OpenXmlFormat.XlsxWorkbook.<ReadWorksheets>d__45.MoveNext()
at ExcelDataReader.ExcelDataReader`2.Reset()
at ExcelDataReader.ExcelOpenXmlReader..ctor(Stream stream, ExcelReaderConfiguration configuration)
at ExcelDataReader.ExcelReaderFactory.CreateOpenXmlReader(Stream fileStream, ExcelReaderConfiguration configuration)

Is there a way to resolve this error?

This issue was fixed in ExcelDataReader Version 3.2.0

Specifically in this commit: https://github.com/ExcelDataReader/ExcelDataReader/commit/b2c6a2050a4a772f28462fa11877d7fda5fe394a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM