忽略使用ExcelDataReader加载到DataSet中的特定列?

时间:2018-06-24 08:55:21

标签: c# exceldatareader

我正在使用ExcelDataReader将数据加载到DataSet中,该数据最终将被加载到WPF中的DataGrid中。我有一些特定的列,其标题为SLNOFY,但我不想包括在内。如何使用here中提到的FilterColumn忽略那些列?

3 个答案:

答案 0 :(得分:1)

如果要在显示时过滤这些列,请使用以下代码:

    gridview.Columns["ColumnName"].Visible = false;

或:

   DataView view = new DataView(DataSet.table[0]);
   DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn",      "ThirdColumn");

   GridDataView.DataSource = Table2;

答案 1 :(得分:0)

您可以处理AutoGeneratingColumn事件:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    e.Cancel = e.PropertyName == "SLNO" || e.PropertyName == "FY";
}

答案 2 :(得分:0)

FilterColumn的工作方式

using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
    List<string> skipColumns = new List<string> { "SLNO", "FY" };
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        var result = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
            {
                UseHeaderRow = true,
                FilterColumn = (rowReader, columnIndex) =>
                {
                    return !skipColumns.Contains((rowReader[columnIndex].ToString()));
                }
            }
        });
    }
}