如何排除我在excelsheet中没有数据的默认命名列,我在数据集中获取该数据

时间:2021-09-16 01:07:59

I am having an excel sheet with multiple rows and columns which I am fetching it in a dataset in my .NET application(using C#). When I see the returned dataset through a simple SELECT query, the excel sheet is having the required columns that I want and also there are some blank columns in between them which by default is given the name F1, F2, F3 and so on. What I want to do is remove these columns and only get the required columns that I want. Thanks in advance.

我有一个包含多行和多列的Excel工作表,我在.NET应用程序的数据集中获取它(使用C#)。当我通过简单的SELECT查询看到返回的数据集时,excel工作表具有我想要的所需列,并且它们之间还有一些空白列,默认情况下给出名称F1,F2,F3等等。我想要做的是删除这些列,只获取我想要的列。提前致谢。

2 个解决方案

#1


2  

Try it with DataTable and DataView:

尝试使用DataTable和DataView:

DataTable oldTable = dataSet.Tables[0];
DataTable newTable = oldTable.DefaultView.ToTable(false, "Only columns you want", "Only columns you want");

With all columns names dynamic you might try this:

所有列名称都是动态的,您可以尝试这样做:

for (int col = dataTable.Columns.Count - 1; col >= 0; col--)
{
  bool removeColumn = true;
  foreach (DataRow row in dataTable.Rows)
  {
    if (!row.IsNull(col))
    {
      removeColumn = false;
      break;
    }
  }
  if (removeColumn)
    dataTable.Columns.RemoveAt(col);
}

Or shorter LinQ version:

或更短的LinQ版本:

for (var col = dataTable.Columns.Count - 1; col >= 0; col--)
    {
        bool removeColumn = dataTable.Rows.Cast<DataRow>().All(row => row.IsNull(col));
        if (removeColumn)
            dataTable.Columns.RemoveAt(col);
    }

#2


0  

@Yeronimo

@Yeronimo

your answer for dynamic columns is working fine... thank you very much sir...

你对动态列的回答很好......非常感谢先生......

and some related articles which might help

和一些可能有帮助的相关文章

Check if rows in an excel sheet are empty with asp.net

使用asp.net检查excel表中的行是否为空

Remove Column From GridView While Converting To Excel

转换为Excel时从GridView中删除列

http://www.daniweb.com/software-development/csharp/threads/297806/how-can-i-remove-empty-columns-in-spreadsheet

http://www.daniweb.com/software-development/csharp/threads/297806/how-can-i-remove-empty-columns-in-spreadsheet

#1


2  

Try it with DataTable and DataView:

尝试使用DataTable和DataView:

DataTable oldTable = dataSet.Tables[0];
DataTable newTable = oldTable.DefaultView.ToTable(false, "Only columns you want", "Only columns you want");

With all columns names dynamic you might try this:

所有列名称都是动态的,您可以尝试这样做:

for (int col = dataTable.Columns.Count - 1; col >= 0; col--)
{
  bool removeColumn = true;
  foreach (DataRow row in dataTable.Rows)
  {
    if (!row.IsNull(col))
    {
      removeColumn = false;
      break;
    }
  }
  if (removeColumn)
    dataTable.Columns.RemoveAt(col);
}

Or shorter LinQ version:

或更短的LinQ版本:

for (var col = dataTable.Columns.Count - 1; col >= 0; col--)
    {
        bool removeColumn = dataTable.Rows.Cast<DataRow>().All(row => row.IsNull(col));
        if (removeColumn)
            dataTable.Columns.RemoveAt(col);
    }

#2


0  

@Yeronimo

@Yeronimo

your answer for dynamic columns is working fine... thank you very much sir...

你对动态列的回答很好......非常感谢先生......

and some related articles which might help

和一些可能有帮助的相关文章

Check if rows in an excel sheet are empty with asp.net

使用asp.net检查excel表中的行是否为空

Remove Column From GridView While Converting To Excel

转换为Excel时从GridView中删除列

http://www.daniweb.com/software-development/csharp/threads/297806/how-can-i-remove-empty-columns-in-spreadsheet

http://www.daniweb.com/software-development/csharp/threads/297806/how-can-i-remove-empty-columns-in-spreadsheet