Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExcelDataReader FilterColumn usage

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

like image 551
Matt Avatar asked Nov 16 '25 13:11

Matt


1 Answers

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
        }
    });
like image 143
user8728340 Avatar answered Nov 18 '25 07:11

user8728340



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!