tags:

views:

160

answers:

4

Hi ,

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;
+1  A: 

Did you try

dt.Columns.Remove[dt.Columns.Count - 1];
Steve Danner
thanks for post ur answer... but this code is not working... and how this line findout that particlular column?
Suryakavitha
+1  A: 

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 ;
Amgad Fahmi
A: 

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

zapping
A: 

dt.Columns.Remove[ColumnIndex];

Or

dt.Columns.Remove["ColumnName"];

try any ...

Lalit