Hari Gillala
Hari Gillala

Reputation: 11926

Column Values Customisation- Export Excel in ASP.NET

I have to export gridview in to the excel sheet 2003. With my code I can Export the data into eXcel Sheet directly. But If there is a null value for the column, Then how do I replace with empty string in Excel sheet. There is a date column which is null, is there any event handler that occurs between export button click and the loading the data into excel. If there is one I can compare the values in the database and can replace the null value with empty String. in the evnt handler.

Please point me in right direction.

Thank you

Upvotes: 0

Views: 1266

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

On click of the export button, you can iterate through your grid rows and replace whatever you need. For example, the code below will replace : with .:

protected void btnExport_Click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    foreach (GridViewRow grdRow in grdProjectTasks.Rows)
    {
        Label lblActualDuration = (Label)grdRow.FindControl("lblActualDuration");

        lblActualDuration.Text = lblActualDuration.Text.Replace(":", ".");
    }

    grdProjectTasks.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();
}

Upvotes: 1

Related Questions