忽略使用 ExcelDataReader 加载到数据集中的特定列?
Ignore specific columns from loading into the DataSet using ExcelDataReader?
我正在使用 ExcelDataReader to load the data into an DataSet
, which will be eventually be loaded into an DataGrid
in WPF. I have specific columns with headers as SLNO
and FY
which I don't want to include. How can I ignore those columns using FilterColumn
mentioned here?
如果要在显示的时候过滤这些列,使用下面的代码:
gridview.Columns["ColumnName"].Visible = false;
或:
DataView view = new DataView(DataSet.table[0]);
DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn", "ThirdColumn");
GridDataView.DataSource = Table2;
您可以处理 AutoGeneratingColumn
事件:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Cancel = e.PropertyName == "SLNO" || e.PropertyName == "FY";
}
这就是 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()));
}
}
});
}
}
我正在使用 ExcelDataReader to load the data into an DataSet
, which will be eventually be loaded into an DataGrid
in WPF. I have specific columns with headers as SLNO
and FY
which I don't want to include. How can I ignore those columns using FilterColumn
mentioned here?
如果要在显示的时候过滤这些列,使用下面的代码:
gridview.Columns["ColumnName"].Visible = false;
或:
DataView view = new DataView(DataSet.table[0]);
DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn", "ThirdColumn");
GridDataView.DataSource = Table2;
您可以处理 AutoGeneratingColumn
事件:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Cancel = e.PropertyName == "SLNO" || e.PropertyName == "FY";
}
这就是 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()));
}
}
});
}
}