开发者

Remove column in datatable

开发者 https://www.devze.com 2022-12-22 11:33 出处:网络
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

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(dvgorderl开发者_运维问答ist, 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 ;


Did you try

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


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 contains all the columns you want in your Excel worksheet. If you prefer you could use a normal string[] instead. I used an array of StringDictionaries because I have more information per row, such as width.

Linq is also very awesome to use for this kinds of tasks, but the example above only supports one row. So I figured we needed some diversity.


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


dt.Columns.Remove[ColumnIndex];

Or

dt.Columns.Remove["ColumnName"];

try any ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号