0

I want to change text in gridView cells before export in excel, because the text is big after export.

This is my button to export in Excel:

 void btnToExcel_Click(object sender, EventArgs e)
    {
        string filename = String.Format("Results_{0}_{1}.xls", DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());
        if (!string.IsNullOrEmpty(tableRef.Page.Title)) filename = tableRef.Page.Title + ".xls";

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.Charset = "";

        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();

        tableRef.Parent.Controls.Add(form);
        form.Controls.Add(tableRef);

        tableResult.Parent.Controls.Add(form);
        form.Controls.Add(tableResult);

        form.RenderControl(htmlWriter);

        HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        HttpContext.Current.Response.Write(stringWriter.ToString());
        HttpContext.Current.Response.End();
    }

enter image description here

I want text after export must be --> Verdana 11px for all cells.

How to do this?

  • 1
    can you add snapshot of text you getting after export, that would be helpful as i am not able to understand text getting Big – Ankitkumar Malde May 3 '16 at 8:33
  • Seen post again.. – Goshky May 3 '16 at 10:43
0

This is solution

int j = 1;
            //Set alternate row color
            tableRef.HeaderRow.Style.Add("font-size", "15");
            foreach (GridViewRow gvrow in tableRef.Rows)
            {
                //gvrow.BackColor = System.Drawing.Color.White;
                if (j <= tableRef.Rows.Count)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("font-size", "11px");
                        gvrow.Cells[k].Style.Add("font-family", "Verdana, Geneva, sans-serif");
                    }
                }
                j++;
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.