2

我正在尝试返回一个数据集,该数据集返回第一行作为标题行(这是有效的),并根据其列标题从数据中过滤掉整个列。

ExcelDataReader 3.4.0介绍了FilterColumn我尝试使用的回调选项。

下面是我的AsDataSet电话

           var ds = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
            {

                // Gets or sets a value indicating whether to use a row from the 
                // data as column names.
                UseHeaderRow = true,

                // Gets or sets a callback to determine which row is the header row. 
                // Only called when UseHeaderRow = true.
                ReadHeaderRow = (rowReader) => {
                    // F.ex skip the first row and use the 2nd row as column headers:
                    rowReader.ToString();
                },

                FilterColumn = (rowReader, columnIndex) => { 
                    return //if header == "string" filter out entire column
                }
            }
        });

上面当我尝试查看行时,列的索引对并测试它是否包含它返回的短语。FilterColumn在这种情况下,正确的用法是什么?

链接到 github:https ://github.com/ExcelDataReader/ExcelDataReader/tree/v3.4.0

4

2 回答 2

3

这真的很容易!只需以这种方式使用它:

FilterColumn = (rowReader, colIndex) => 
    rowReader[colIndex].ToString() != "string"

或者

FilterColumn = (rowReader, colIndex) => 
    !rowReader[colIndex].ToString().Contains("string")
于 2018-08-14T10:13:09.377 回答
2

将标题放在列表中ReadHeaderRow,并检查索引FilterColumn

    var headers = new List<string>();

    ds = reader.AsDataSet(new ExcelDataSetConfiguration()
    {
        ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
        {
            UseHeaderRow = true,

            ReadHeaderRow = rowReader => {
                for (var i = 0; i < rowReader.FieldCount; i++)
                    headers.Add(Convert.ToString(rowReader.GetValue(i)));
            },

            FilterColumn = (columnReader, columnIndex) =>
                headers.IndexOf("string") != columnIndex
        }
    });
于 2018-04-18T21:55:09.567 回答