我试图返回一个数据集,该数据集将第一行作为标题行返回(这是有效的),并根据列标题从数据中筛选出整个列。
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的链接
发布于 2018-04-18 21:55:09
将标题放在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-08-14 10:13:09
这真的很容易!用这种方式就行了:
FilterColumn = (rowReader, colIndex) =>
rowReader[colIndex].ToString() != "string"或
FilterColumn = (rowReader, colIndex) =>
!rowReader[colIndex].ToString().Contains("string")https://stackoverflow.com/questions/49908729
复制相似问题