【问题标题】:C# ExcelDataReader read from specific columns onlyC# ExcelDataReader 仅从特定列读取
【发布时间】:2019-03-15 15:14:02
【问题描述】:

我正在尝试从我的 excel 表中获取数据以将其添加到数据库中,这可以完美运行,但我只想要特定标题下的数据。 这是我的配置代码:

var headers = new List<string>;
DataSet result = excelDataReader.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("LOCATION") == columnIndex
                    || headers.IndexOf("PARENT") == columnIndex
                    || headers.IndexOf("DESCRIPTION") == columnIndex
            }
        });

LOCATION,PARENTDESCRIPTION 是列标题名称。 这是我用来将数据添加到数据库的代码

foreach (DataTable table in result.Tables)
        {
            foreach (DataRow row in table.Rows)
            {

                if (!existedLocations.Any(l => l.ShortCode?.Replace(" ", String.Empty) == row[0].ToString().Replace(" ", String.Empty)))
                {
                    addtable.Name = addtable.NameAr = row[2].ToString().Substring(row[2].ToString().LastIndexOf(',') + 1);
                    addtable.ParentLocation = connection.Locations.FirstOrDefault(l => l.ShortCode == row[1].ToString()).Id;
                    addtable.LocationType = (int)LocationsTypes.Area;
                    addtable.ShortCode = row[0].ToString();
                    addtable.Category = (int)LocationsCategory.indoor;
                    addtable.IsActive = 1;
                    addtable.Isdeleted = 0;
                    existedLocations.Add(addtable);
                    connection.Locations.InsertOnSubmit(addtable);
                    connection.SubmitChanges();
                    addtable = new Location();
                }
            }
        }

工作表标题定义如下 sheet1

sheet2

【问题讨论】:

  • 那么,有什么问题呢?您的result 不只包含您在FilterColumn 中提供的列?
  • FilterColumn 是否删除了给定的列? @磁控管
  • 反之亦然,如果您想要该列,则返回 true,否则返回 false。 Check the Github pageFilterColumn获取或设置回调以确定是否将特定列包含在 DataTable 中。读取标题后每列调用一次。

标签: c# excel desktop-application exceldatareader


【解决方案1】:

嗯,你有两张表,标题相同,但位置不同。您的代码将第一张工作表的标题添加到列表中,然后将第二张工作表中的标题添加到列表中。因此,当您在第二张表中查找要过滤的标题时,您会从第一个表中获取索引,因为 IndexOf 将获得第一次出现。

此外,您似乎只使用headers 列表来过滤列,因此您可以简化:

var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
    ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
    {
        UseHeaderRow = true,

        FilterColumn = (columnReader, columnIndex) =>
        {
            string header = columnReader.GetString(columnIndex);
            return (header == "LOCATION" || 
                    header == "PARENT" || 
                    header == "DESCRIPTION"
                   );                        
        }           
    }
});

【讨论】:

  • 它仍在获取没有指定标题的列@Magnetron
  • @MohammedEhab 这很奇怪,因为它对我有用。您传递的是您想要的标题还是您不想要的标题?另外,标题名称是正确的,包括大小写?表头是否在工作表的第一行?
  • 是的,但我认为 Index 存在问题,因为每张表中标题的顺序不同
  • @MohammedEhab 好的,所以您有多个具有相同标题但顺序不同的工作表,这就是问题所在。检查我的编辑,我的第二个解决方案应该适合你。我将进行另一次编辑,以便更清楚并删除不起作用的部分。
  • 我刚刚添加了header.ToLower() == "location" || header.ToLower() == "parent" ||header.ToLower() == "description" 以更好地控制列名
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2016-02-28
  • 2010-11-14
相关资源
最近更新 更多