如何跳过某个范围内的空白列?

发布于 2025-01-06 17:42:30 字数 1273 浏览 0 评论 0原文

我使用 C# 和 Interop.Excel.Range 返回工作表的使用的列范围,并将使用的列和行写入数据网格视图。某些列返回空白/空。

Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;

TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
DataGridView wDGV = new DataGridView();
wDGV.Dock = DockStyle.Fill;
wTabPage.Controls.Add(wDGV);
Sheets_TabControl.TabPages.Add(wTabPage);

DataTable dt = new DataTable();
DataRow wNewRow = null;

for (int i = 0; i < excelRange.Columns.Count; i++)
{
    dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
}

string wValue = string.Empty;
Microsoft.Office.Interop.Excel.Range wRange = null;

for (int wRowIndex = 1; wRowIndex <= skipRows; wRowIndex++)
{
    wNewRow = dt.NewRow();

    foreach (DataColumn wColumn in dt.Columns)
    {
        wRange = excelRange.Cells[wRowIndex, wColumn.Ordinal + 1];

        if (wRange != null)
        {
            if (wRange.Value2 != null)
            {
                wValue = wRange.Value2.ToString();

                if (!string.IsNullOrEmpty(wValue))
                {
                    wNewRow.SetField(wColumn, wValue);
                }
            }

        }
    }

    dt.Rows.Add(wNewRow)
}

wDGV.DataSource = dt;

在写入包含数据的列时,是否有办法跳过或忽略任何空白列?

感谢您的帮助!

I'm using C# and Interop.Excel.Range to return a the used range of columns for a worksheet and write the used columns and rows to a datagrid view. Some of the columns are returning blank/null.

Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;

TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
DataGridView wDGV = new DataGridView();
wDGV.Dock = DockStyle.Fill;
wTabPage.Controls.Add(wDGV);
Sheets_TabControl.TabPages.Add(wTabPage);

DataTable dt = new DataTable();
DataRow wNewRow = null;

for (int i = 0; i < excelRange.Columns.Count; i++)
{
    dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
}

string wValue = string.Empty;
Microsoft.Office.Interop.Excel.Range wRange = null;

for (int wRowIndex = 1; wRowIndex <= skipRows; wRowIndex++)
{
    wNewRow = dt.NewRow();

    foreach (DataColumn wColumn in dt.Columns)
    {
        wRange = excelRange.Cells[wRowIndex, wColumn.Ordinal + 1];

        if (wRange != null)
        {
            if (wRange.Value2 != null)
            {
                wValue = wRange.Value2.ToString();

                if (!string.IsNullOrEmpty(wValue))
                {
                    wNewRow.SetField(wColumn, wValue);
                }
            }

        }
    }

    dt.Rows.Add(wNewRow)
}

wDGV.DataSource = dt;

Is there a way to skip or ignore any columns that are blank, while writing those columns that contain data?

Thanks for the help!

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

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

发布评论

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

评论(1

棒棒糖 2025-01-13 17:42:30

关键是使用 VBA 工作表函数 CountA,该函数返回一个值,指示给定范围内有多少个单元格有数据。基于此,您可以决定是否在 DataTable 中创建列。

由于表列和 Excel 范围列现在不同步,因此您需要使用在代码中设置的列名称作为相应 Excel 范围列的索引。

因此,重新调整代码可以得到:

Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;

TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
DataGridView wDGV = new DataGridView();
wDGV.Dock = DockStyle.Fill;
wTabPage.Controls.Add(wDGV);
Sheets_TabControl.TabPages.Add(wTabPage);

DataTable dt = new DataTable();
DataRow wNewRow = null;

// New code to create DataTable columns

for (int i = 1; i <= excelRange.Columns.Count; i++)
{
    // If the current column of cells does not contain any data, then don't add a column to the datatable

    if (xlSpreadsheet.App.WorksheetFunction.CountA(excelRange.Cells[Missing.Value, i]) != 0)
    {
        dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
    }
}

string wValue = string.Empty;
Microsoft.Office.Interop.Excel.Range wRange = null;

for (int wRowIndex = 1; wRowIndex <= excelRange.Rows.Count; wRowIndex++)
{
    wNewRow = dt.NewRow();

    foreach (DataColumn wColumn in dt.Columns)
    {
        // Parse the column number we stored earlier as the column name

        int colNumber = 0;

        if (int.TryParse(wColumn.ColumnName, out colNumber))
        {
            // We use the parsed column number to index the column in the Excel range

            wRange = excelRange.Cells[wRowIndex, colNumber];

            if (wRange != null)
            {
                if (wRange.Value2 != null)
                {
                    wValue = wRange.Value2.ToString();

                    if (!string.IsNullOrEmpty(wValue))
                    {
                        wNewRow.SetField(wColumn, wValue);
                    }
                }
            }
        }
    }

    dt.Rows.Add(wNewRow)
}

wDGV.DataSource = dt;

The key to this is to use the VBA worksheet function CountA which returns a value indicating how many cells in a given range have data. Based on this you can decide whether or not to create a column in your DataTable.

Since your table columns and Excel range columns are now out of sync, you'll need to use the column name you set in your code as the index of the corresponding Excel range column.

So, rejigging your code gives:

Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;

TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
DataGridView wDGV = new DataGridView();
wDGV.Dock = DockStyle.Fill;
wTabPage.Controls.Add(wDGV);
Sheets_TabControl.TabPages.Add(wTabPage);

DataTable dt = new DataTable();
DataRow wNewRow = null;

// New code to create DataTable columns

for (int i = 1; i <= excelRange.Columns.Count; i++)
{
    // If the current column of cells does not contain any data, then don't add a column to the datatable

    if (xlSpreadsheet.App.WorksheetFunction.CountA(excelRange.Cells[Missing.Value, i]) != 0)
    {
        dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
    }
}

string wValue = string.Empty;
Microsoft.Office.Interop.Excel.Range wRange = null;

for (int wRowIndex = 1; wRowIndex <= excelRange.Rows.Count; wRowIndex++)
{
    wNewRow = dt.NewRow();

    foreach (DataColumn wColumn in dt.Columns)
    {
        // Parse the column number we stored earlier as the column name

        int colNumber = 0;

        if (int.TryParse(wColumn.ColumnName, out colNumber))
        {
            // We use the parsed column number to index the column in the Excel range

            wRange = excelRange.Cells[wRowIndex, colNumber];

            if (wRange != null)
            {
                if (wRange.Value2 != null)
                {
                    wValue = wRange.Value2.ToString();

                    if (!string.IsNullOrEmpty(wValue))
                    {
                        wNewRow.SetField(wColumn, wValue);
                    }
                }
            }
        }
    }

    dt.Rows.Add(wNewRow)
}

wDGV.DataSource = dt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文