6

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();
        }
    }
}
Ganesh bagad
  • 83
  • 1
  • 2
  • 7
  • Could you please let me know whether the answer solves the problem you 've described. Do you have other issues with the approach I've described? Cheers! – Sergii Zhevzhyk Dec 17 '15 at 14:59
  • Ganesh, what @SergiiZhevzhyk is politely trying to say is that you should play by the rules and mark an answer as the official answer if it solves your problem. http://stackoverflow.com/help/someone-answers – Francois Botha Feb 14 '17 at 10:06

1 Answers1

15

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);
Sergii Zhevzhyk
  • 4,074
  • 22
  • 28