Question

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?

Était-ce utile?

La solution

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++;
            }
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top