ExcelDataReader FilterColumn usage

核能气质少年 提交于 2019-12-23 01:42:11

问题


I am trying to return a data set that returns the first row as the header row(this is working) as well as filter out entire columns from the data depending on their column header.

ExcelDataReader 3.4.0 introduced the FilterColumn callback option which I am attempting to utilize.

Below is my AsDataSet call

           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
                }
            }
        });

Above when I try to look at the row,index pair of the column and test to see if it contains the phrase it returns it anyway. What is the proper usage of FilterColumn in this scenario?

Link to github : https://github.com/ExcelDataReader/ExcelDataReader/tree/v3.4.0


回答1:


Put the headers in a list in ReadHeaderRow, and check the index in 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
        }
    });



回答2:


It is really easy! Just use it in this way:

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

or

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


来源:https://stackoverflow.com/questions/49908729/exceldatareader-filtercolumn-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!