【问题标题】:Remove column in datatable删除数据表中的列
【发布时间】:2010-03-11 13:39:03
【问题描述】:

我正在成功地将数据表导出到 excel 表中...在该 excel 表中,我必须显示除最后一列之外的数据表的列(customerid、Productname、referenceno)...现在我该如何显示excel中的数据表不显示最后一列(referenceno)...

谁能告诉我这个问题的解决方案.. 提前谢谢..

这是我将数据表导出到 excel 的代码:

         System.Data.DataTable dt = clsobj.convert_datagrid_orderlist_to_datatable(dvgorderlist, txtreferenceno);

        oxl = new Excel.Application();
        oxl.Visible = true;
        oxl.DisplayAlerts = false;

        wbook = oxl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        oxl.ActiveCell.set_Item(2, 4, "Alfa Aesar");

        wsheet = (Excel.Worksheet)wbook.ActiveSheet;

        wsheet.Name = "Customers";
        Excel.Range range = wsheet.get_Range("A6", "H6");

        wsheet.get_Range("A6", "H6").Font.Name = "Times new Roman";
        wsheet.get_Range("A6", "H6").Font.Size = 12;
        wsheet.get_Range("A6", "H6").Interior.Color = ConvertColour(Color.SkyBlue);

        oxl.ActiveWindow.DisplayGridlines = false;

        int rowCount = 5;
        foreach (DataRow dr in dt.Rows)
        {
            rowCount += 1;
            for (int i = 1; i < dt.Columns.Count + 1; i++)
            {
                // Add the header the first time through
                if (rowCount == 7)
                {
                    wsheet.Cells[6, i] = dt.Columns[i - 1].ColumnName;

                }
                wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
                Excel.Range cellRange = (Range)wsheet.Cells[rowCount, i];
                //cellRange.Interior.Color = 200;
                //cellRange.Interior.Color = ConvertColour(Color.LightBlue);
                cellRange.Cells.Borders.LineStyle = BorderStyle.FixedSingle;
            }


        }

        cells = wsheet.get_Range(wsheet.Cells[2, 2],
                      wsheet.Cells[rowCount, dt.Columns.Count]);
        cells.EntireColumn.AutoFit();


        wsheet = null;
        cells = null;

【问题讨论】:

  • 你想在excel中隐藏它,还是根本不设置列?

标签: c# excel vba


【解决方案1】:

在你的 for 语句中改变这一行

 for (int i = 1; i < dt.Columns.Count + 1; i++)

有了这个

var filteredColumns = dt.Columns.OfType<DataColumn>()
         .Where( x=> x.ColumnName != "referenceno" );
              foreach (var column in filteredColumns )
{
     //do whatever you want 
             //if you need the index you can create counter and increase it by 1 each loop 
}

别忘了使用 linq

using System.Linq ;

【讨论】:

    【解决方案2】:

    你试过了吗

    dt.Columns.Remove[dt.Columns.Count - 1];
    

    【讨论】:

    • 感谢您的回答...但是此代码不起作用...以及此行如何找到该特定列?
    【解决方案3】:
    foreach (DataColumn dc in dt.Columns)
    {
        bool deleteIt = true;
        foreach (StringDictionary sd in sdArray)
        {
            if (dc.ColumnName.Equals(sd["Name"]))
                deleteIt = false;
        }
        if (deleteIt)
            data.Columns.Remove(dc);
    }
    

    sdArray 包含您在 Excel 工作表中所需的所有列。如果您愿意,可以改用普通的string[]。我使用了StringDictionaries 的数组,因为我每行有更多信息,例如宽度。

    Linq 也非常适合用于此类任务,但上面的示例仅支持一行。所以我认为我们需要一些多样性。

    【讨论】:

      【解决方案4】:

      试试Worksheet.get_Range("rangeVal", "rangeVal").EntireColumn.Hidden = true;

      【讨论】:

        【解决方案5】:

        dt.Columns.Remove[ColumnIndex];

        或者

        dt.Columns.Remove["ColumnName"];

        尝试任何...

        【讨论】:

          猜你喜欢
          • 2012-09-21
          • 1970-01-01
          • 1970-01-01
          • 2019-06-07
          • 1970-01-01
          • 1970-01-01
          • 2018-04-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多