Modify Excel format exported by GridView

0

I'm doing a project in which I upload data to my GridView (Inserted Data) from my Database.

I export my GV in an Excel with this code:

private void ExportarGridExcel()
{
   int num = 0;
   Response.Clear();
   Response.Buffer = true;
   Response.ClearContent();
   Response.ClearHeaders();
   Response.Charset = "";
   lblcantidad.Text = num.ToString();
   string FileName = "Usuarios y Contraseñas" + DateTime.Now + ".xls";
   StringWriter strwritter = new StringWriter();
   HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.ContentType = "application/vnd.ms-excel";
   Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
   GrillaDatosInsertados.GridLines = GridLines.Both;
   GrillaDatosInsertados.HeaderStyle.Font.Bold = true;
   GrillaDatosInsertados.RenderControl(htmltextwrtter);
   Response.Write(strwritter.ToString());
   Response.End();
}

protected void Button2_Click3(object sender, EventArgs e)
{
  ExportarGridExcel();
}

This is the interface:

By pressing the button, the gridview is exported.

What I want to do is format text to all the cells, since the password field gets numbers, and some have 0 at the beginning, excel recognizes as if it were a field number, and you delete that 0

    
asked by JiMel 07.12.2018 в 23:13
source

1 answer

0

Well I found the solution in another Google site. I'll share it with you in case someone needs it.

I inserted this code in the RowDataBound

event
protected void GrillaDatosInsertados_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }
    }

Makes all rows in the Gridview text format.

    
answered by 10.12.2018 в 17:25