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

Ganesh bagad picture Ganesh bagad · Dec 12, 2015 · Viewed 8k times · Source

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();
        }
    }
}

Answer

Sergii Zhevzhyk picture Sergii Zhevzhyk · Dec 13, 2015

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);