Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove header from datatable at the time of export to Excel?

Tags:

c#

closedxml

From a datatable I want to remove headers. How can I remove headers or first row which includes the headers.

if (dt.Rows.Count > 0)
{
    using (XLWorkbook wb = new XLWorkbook())
    {
        wb.Worksheets.Add(dt, "Customers");

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        Response.AddHeader("content-disposition", "attachment;filename=" + fname + ".xlsx");

        using (MemoryStream MyMemoryStream = new MemoryStream())
        {
            wb.SaveAs(MyMemoryStream);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
    }
}
like image 250
Ganesh bagad Avatar asked Dec 12 '15 08:12

Ganesh bagad


People also ask

How do I remove a header from a CSV file in Excel?

In the List properties, under General > Column Titles, change the setting to 'Hide". Then go to under General > Column Titles, change the setting to 'Hide". or simply select the header (by going to ancestor select list column title style ) and box type none or cut it.

How do I export a header in Excel?

It is possible to generate Excel exports with some static images or text in the Excel's headers. The steps are: Start with a new or existing Excel template and click on Insert menu and there click on Header & Footer submenu. This adds Header & Footer to your Excel document.


1 Answers

If you don't want to see the headers in the worksheet, don't add the whole table, but only the data. For example, the task is to add all rows of the dt table starting from the first cell of the first row in the worksheet:

var ws = wb.Worksheets.Add("Customers");
ws.FirstRow().FirstCell().InsertData(dt.Rows);
like image 162
Sergii Zhevzhyk Avatar answered Nov 09 '22 23:11

Sergii Zhevzhyk