Search code examples
c#asp.net.netexport-to-excel

Remove first row contents in excel while export to excel


I'm using the below code to export the gridview to excel. When I export the contents are appearing fine with no problems, but I am getting a control label like System.WebControls.UI.Style this in my first cell in A1 cell.

Is there a way to hide this or remove and below is my code.

CS:

protected void Unnamed1_Click(object sender, ImageClickEventArgs e)
{
    // ExportToExcel();
    if (ViewState["DashboardDetails"] != null)
    {
        lblError.Text = string.Empty;
        lblError.Style.Add("display", "None");

        grdHorizontalSeatDashboard.AllowPaging = false;
        DataTable dtHorizontalDashboard = (DataTable)ViewState["DashboardDetails"];
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=HorizontalDashboard.xls");
        Response.Charset = "utf-8";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            grdHorizontalSeatDashboard.AllowPaging = false;
            grdHorizontalSeatDashboard.DataSource = (DataTable)ViewState["DashboardDetails"];
            grdHorizontalSeatDashboard.DataBind();
            grdHorizontalSeatDashboard.HeaderRow.BackColor = Color.White;

            //Horizontal Cell
            TableCell cell0 = grdHorizontalSeatDashboard.HeaderRow.Cells[0];
            cell0.BackColor = ColorTranslator.FromHtml("#9AD6EE");
            cell0.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[0].Width;
            TableCell cell1 = grdHorizontalSeatDashboard.HeaderRow.Cells[1];
            cell1.BackColor = ColorTranslator.FromHtml("#96B060");
            cell1.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[1].Width;
            TableCell cell2 = grdHorizontalSeatDashboard.HeaderRow.Cells[2];
            cell2.BackColor = ColorTranslator.FromHtml("#96B060");
            cell2.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[2].Width;
            TableCell cell3 = grdHorizontalSeatDashboard.HeaderRow.Cells[3];
            cell3.BackColor = ColorTranslator.FromHtml("#96B060");
            cell3.Width = Unit.Pixel(100);
            TableCell cell4 = grdHorizontalSeatDashboard.HeaderRow.Cells[4];
            cell4.BackColor = ColorTranslator.FromHtml("#C57838");
            cell4.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[4].Width;
            TableCell cell5 = grdHorizontalSeatDashboard.HeaderRow.Cells[5];
            cell5.BackColor = ColorTranslator.FromHtml("#C57838");
            cell5.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[5].Width;
            TableCell cell6 = grdHorizontalSeatDashboard.HeaderRow.Cells[6];
            cell6.BackColor = ColorTranslator.FromHtml("#C57838");
            cell6.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[6].Width;       

           // Likely $ Impact & Overall Horizontal Utilization

            TableCell cell10 = grdHorizontalSeatDashboard.HeaderRow.Cells[10];
            cell10.BackColor = ColorTranslator.FromHtml("#9AD6EE");
            cell10.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[10].Width;
            TableCell cell11 = grdHorizontalSeatDashboard.HeaderRow.Cells[11];
            cell11.BackColor = ColorTranslator.FromHtml("#9AD6EE");
            cell11.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[11].Width;        
            foreach (GridViewRow row in grdHorizontalSeatDashboard.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    cell.BackColor = row.BackColor;
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    cell.CssClass = "textmode";
                }
            }

            foreach (TableCell cell in grdHorizontalSeatDashboard.FooterRow.Cells)
            {
                cell.BackColor = grdHorizontalSeatDashboard.FooterRow.BackColor;
                cell.HorizontalAlign = HorizontalAlign.Center;
            }
            Style style = new Style();
            style.BackColor = Color.White;    
            Response.Write(style);
            grdHorizontalSeatDashboard.RenderControl(hw);           
            Response.Output.Write(sw.ToString());                 
            Response.Flush();
            Response.End();
        }
    }
}

Solution

  • To hide the first column or the first row (A1), just count the current row/column iteration and intercept the first and hide the A1 cell.

    var rowCounter = 0;
    var cellCounter = 0;
    foreach (GridViewRow row in grdHorizontalSeatDashboard.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            if (rowCounter == 0 && cellCounter == 0) 
            {
                cell.Text = "";
            }
    
            cell.BackColor = row.BackColor;
            cell.HorizontalAlign = HorizontalAlign.Center;
            cell.CssClass = "textmode";
        }
        cellCounter = 0;
        rowCounter++;
    }