在数据表中删除列 [英] Remove column in datatable

查看:115
本文介绍了在数据表中删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导出一个DataTable到Excel表成功......在这个Excel工作表。我除了最后一栏显示的数据表中的列(客户ID,产品名称,referenceno)....现在我怎么能显示在Excel中的数据表,而不显示最后一列(referenceno)...



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



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

 系统.Data.DataTable DT = clsobj.convert_datagrid_orderlist_to_datatable(dvgorderlist,txtreferenceno); 

OXL =新Excel.Application();
oxl.Visible = TRUE;
oxl.DisplayAlerts = FALSE;

wbook = oxl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
oxl.ActiveCell.set_Item(2,4,阿法埃莎);

wsheet =(Excel.Worksheet)wbook.ActiveSheet;

wsheet.Name =客户;
Excel.Range范围= wsheet.get_Range(A6,H6);

wsheet.get_Range(A6,H6)Font.Name =宋体。
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(在dt.Rows的DataRow DR)
{
rowCount时+ = 1;
的for(int i = 1; I< dt.Columns.Count + 1;我++)
{
//添加标题在第一时间通过
如果(rowCount等= = 7)
{
wsheet.Cells [6,I] = dt.Columns [我 - 1] .ColumnName;

}
wsheet.Cells [rowCount时,我] =博士[I - 1]的ToString();
Excel.Range cellRange =(范围)wsheet.Cells [rowCount时,我]
//cellRange.Interior.Color = 200;
//cellRange.Interior.Color = ConvertColour(Color.LightBlue);
cellRange.Cells.Borders.LineStyle = BorderStyle.FixedSingle;
}


}

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


wsheet = NULL;
细胞= NULL;


解决方案

在你的声明中改变这一行



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

与此一

  VAR filteredColumns = DT .Columns.OfType<&DataColumn的GT;()
。凡(X =>!x.ColumnName =referenceno);
的foreach(在filteredColumns VAR列)
{
//做你想做的
//如果需要,您可以创建计数器和1每次循环$ B增加它的指数$ b}

不要忘了使用LINQ

 使用System.Linq的; 


I am exporting a datatable to an excel sheet successfully... In that excel sheet i have to display the columns(customerid,Productname,referenceno) of the data table except the last column.... now how can i display the data table in excel without display the last column(referenceno)...

anyone tell me the solution of this problem.. Thanks in Advance..

here is my code for export datatable to 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;

解决方案

In your for statement change this line

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

with this one

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 
}

don't forget to use linq

using System.Linq ;

这篇关于在数据表中删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆