Umm E Habiba Siddiqui
Umm E Habiba Siddiqui

Reputation: 594

Replace checkboxes while exporting grid to excel

I don't want to show checkboxes when I export my grid to excel. I header checbox doesn't show while all child chechboxes are always there.. :( I debug and found out the reason that gridview concat its own id in name to each row so that my code couldn't detect checkboxes. here is my code

 protected void Export_to_Excel(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.DataSource = dtPatientList;
        GridView1.DataBind();
        System.Diagnostics.Debugger.Break();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=PatientSearchReport.xls");
        Response.ContentType = "application/vnd.xlsx";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        string html2 = Regex.Replace(stringWrite.ToString(), @"(<input type=""image""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<input type=""checkbox""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<a \/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        Response.Write(html2.ToString());
        Response.End();
        GridView1.AllowPaging = true;
        GridView1.DataSource = dtPatientList;
        GridView1.DataBind();
    }

Upvotes: 0

Views: 150

Answers (1)

user2568239
user2568239

Reputation:

Try

protected void Export_to_Excel(object sender, EventArgs e)
    {
        //System.Diagnostics.Debugger.Break();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=PatientSearchReport.xls");
        Response.ContentType = "application/vnd.xlsx";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);  
        GridView1.RenderControl(htmlWrite);
        string html2 = Regex.Replace(stringWrite.ToString(), @"(<input type=""image""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<input class=""checkbox""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<a \/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        Response.Write(html2.ToString());
        Response.End();
    }

Upvotes: 1

Related Questions