尝试使用 .csv 读取具有空行(通常在末尾)的 CSV 文件CsvHelper.GetRecords<T>()。
如果没有空行,这是一种享受。但是,如果 CSV 文件有一个空行(定义为 , , , , , ),那么它会抛出一个TypeConverterException
Text: ''
MemberType: IntelligentEditing.PerfectIt.Core.DataTypes.Styles.StyleRuleType
TypeConverter: 'CsvHelper.TypeConversion.EnumConverter'
我已经阅读了文档(https://joshclose.github.io/CsvHelper/api/CsvHelper.Configuration/Configuration/)并尝试将配置对象设置为IgnoreBlankLines = true但是这不起作用。
简化为一个例子:
Text: ''
MemberType: IntelligentEditing.PerfectIt.Core.DataTypes.Styles.StyleRuleType
TypeConverter: 'CsvHelper.TypeConversion.EnumConverter'
CSV 通常包含如下内容:
Id,Value,ItemType
1,This,Unknown
2,That,Accounts
3,Other,HR
,,
,,
我希望IgnoreBlankLines忽略 CSV 中的空白行,但事实并非如此。有任何想法吗?
Bow*_*wen 13
除了@David Specht 的回答。新版本更新了 delegate ShouldSkipRecord。我使用的是 28.0.1 版本,下面的代码适用于我。
ShouldSkipRecord = args => args.Row.Parser.Record.All(string.IsNullOrWhiteSpace)
@phat.huynh 有正确的想法。只需告诉它跳过所有字段都是空字符串的任何记录。
var configuration = new Configuration()
{
    HasHeaderRecord = true,
    HeaderValidated = null,
    MissingFieldFound = null,
    ShouldSkipRecord = (record) => record.All(field => string.IsNullOrEmpty(field))     
};
您可以尝试在配置上实现 ShouldSkipRecord 来选择跳过或不跳过
var configuration = new Configuration () {
                HasHeaderRecord = true,
                HeaderValidated = null,
                MissingFieldFound = null,
                IgnoreBlankLines = true,
                ShouldSkipRecord = (records) =>
                {
                    // Implement logic here
                    return false;
                }
            };
| 归档时间: | 
 | 
| 查看次数: | 4286 次 | 
| 最近记录: |