MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

Value : , it can't cast to Nullable`1 type.

Open ExcelHelper opened this issue 4 years ago • 3 comments

Excel Type

  • [ ] CSV

Upload Excel File

Please attach your issue file by dragging or droppng, selecting or pasting them. 22.XLS

MiniExcel Version

最新

Description

‘using (var stream = File.OpenRead(path)) { var xxx = stream.Query<ForSales>(excelType: ExcelType.CSV, configuration: new CsvConfiguration() { Seperator = '\t', StreamReaderFunc = (stream) => new StreamReader(stream, Encoding.GetEncoding(936)) }); }’ xxx的resultview,会出现:ColumnName : 成交金额, CellRow : 2, Value : , it can't cast to Nullable`1 type.,我不清楚是不是BUG

ExcelHelper avatar Sep 10 '21 02:09 ExcelHelper

我这边测试结果(如下图),请问代码逻辑是一样吗 image

shps951023 avatar Sep 10 '21 03:09 shps951023

是的,这个可以出来的,但如果继续 .Where(fs =>Convert.ToDouble(fs.成交金额)==0 ); 就会弹出 ColumnName : 成交金额, CellRow : 2, Value : , it can't cast to Nullable`1 type.

ExcelHelper avatar Sep 10 '21 03:09 ExcelHelper

因为后面数据有空白,MiniExcel csv dynamic query预设型别为string,所以有此问题 image

可以增加如下判断,或是使用强型别Query

	using (var stream = File.OpenRead(path))
	{
		var xxx = stream.Query(useHeaderRow:true, excelType: ExcelType.CSV, configuration: new CsvConfiguration() { 
			Seperator = '\t', StreamReaderFunc = (stream) => new StreamReader(stream, Encoding.GetEncoding(936)) 
		});
		Console.WriteLine(xxx.Where(fs =>(string.IsNullOrEmpty(fs.成交金额)?0:Convert.ToDouble(fs.成交金额))==0 ));
	}

image

shps951023 avatar Sep 10 '21 07:09 shps951023