使用 ExcelDataReader.CreateReader() 时如何解决 DateTime 解析的 System.FormatException 错误?
How can I resolve a System.FormatException error on DateTime parsing when using ExcelDataReader.CreateReader()?
我从供应商那里收到一个 .xlsx 文件,它包含一个 HH:mm:ss 格式的持续时间列,当我以 Excel 打开文件时,它显示为日期,但实际上应该只是一个字符串。
我想使用 ExcelDataReader 将此 .xslx 文件读入我的 c# 应用程序,而无需在 运行 我的应用程序之前手动修改或删除该列。
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
然而,当我尝试如上所述创建 reader 时,出现以下错误:
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)
有没有办法解决这个错误?
此问题已在 ExcelDataReader
版本 3.2.0
中修复
特别是在这个提交中:
https://github.com/ExcelDataReader/ExcelDataReader/commit/b2c6a2050a4a772f28462fa11877d7fda5fe394a
我从供应商那里收到一个 .xlsx 文件,它包含一个 HH:mm:ss 格式的持续时间列,当我以 Excel 打开文件时,它显示为日期,但实际上应该只是一个字符串。
我想使用 ExcelDataReader 将此 .xslx 文件读入我的 c# 应用程序,而无需在 运行 我的应用程序之前手动修改或删除该列。
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
然而,当我尝试如上所述创建 reader 时,出现以下错误:
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)
有没有办法解决这个错误?
此问题已在 ExcelDataReader
版本 3.2.0
特别是在这个提交中: https://github.com/ExcelDataReader/ExcelDataReader/commit/b2c6a2050a4a772f28462fa11877d7fda5fe394a