删除数据集中的空行

发布于 12-05 11:52 字数 625 浏览 1 评论 0原文

如何删除数据集中的空行?

我正在从 Excel 电子表格中读取数据,该电子表格底部有一些空行。

到目前为止,这是我的代码:

ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", VariableFile);

OleDbConnection objConn = new OleDbConnection(ConnectionString);

objConn.Open();

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Requirements$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1);

objConn.Close();

How do I delete the empty rows in my dataset?

I am reading data from an excel spreadsheet that has a few empty rows at the bottom.

Here is my code so far:

ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", VariableFile);

OleDbConnection objConn = new OleDbConnection(ConnectionString);

objConn.Open();

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Requirements$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1);

objConn.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韬韬不绝2024-12-12 11:52:24

为什么不直接修改查询以仅提取非空数据。

Why not just modify your query to pull only the non-empty data.

捂风挽笑2024-12-12 11:52:24

我找到的每个解决方案都告诉我像您一样修改 Excel 查询。所以这没有多大帮助。您可以从表中创建一个数据视图来查看非空白行。您事先知道这些列是什么吗?即使不这样做,您仍然可以循环列名并为 DataView 构建过滤器字符串。

string filter = "";

foreach (DataColumn dc in dt.Columns)
{
    filter += dc.ColumnName + " <> '' ";

    if (dt.Columns[dt.Columns.Count-1].ColumnName != dc.ColumnName)
    {
        filter += " AND ";
    }
}

DataView view = new DataView(dt);
view.RowFilter = filter;
dt = view.ToTable();

Every solution I have found told me to modify the Excel query like you have it. So that isn't much help. You could just create a DataView from your Table that would look at the non-blank rows. Do you know what the columns are beforehand? Even if you don't you could still loop over the column names and build a filter string for the DataView.

string filter = "";

foreach (DataColumn dc in dt.Columns)
{
    filter += dc.ColumnName + " <> '' ";

    if (dt.Columns[dt.Columns.Count-1].ColumnName != dc.ColumnName)
    {
        filter += " AND ";
    }
}

DataView view = new DataView(dt);
view.RowFilter = filter;
dt = view.ToTable();
夏日浅笑〃2024-12-12 11:52:24

使用 Linq 仅包含任何列中具有非空/空白值的行。

var filteredData = sourceDataTable.AsEnumerable()
         .Where(row => row.ItemArray.Any(col => !String.IsNullOrWhiteSpace(col.ToString())))
         .ToArray();

Use Linq to include only rows that have a non-null/blank value in any of its columns.

var filteredData = sourceDataTable.AsEnumerable()
         .Where(row => row.ItemArray.Any(col => !String.IsNullOrWhiteSpace(col.ToString())))
         .ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文