lundi 19 août 2019

asp.net file exporting to excel nothing happens

I'm trying to export my data in my asp.net project and i'm stock with it for a couple of days. My code seems good and i don't know why its is not working.

this is my code of exporting:

dt = transaction.getTransaction(dateFrom, dateTo);
 if (dt.Rows.Count > 0)
 {
     HttpContext.Current.Response.ClearContent();
     HttpContext.Current.Response.Buffer = true;
     HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
     HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename = trans.xls"));
  HttpContext.Current.Response.Charset = "";

              dgv.DataSource = dt;
              dgv.DataBind();
      using (StringWriter sw = new StringWriter()){

             using (HtmlTextWriter hsw = new HtmlTextWriter(sw)){


                   dgv.RenderControl(hsw);
                HttpContext.Current.Response.Output.Write(sw.ToString());
                HttpContext.Current.Response.Flush();
               HttpContext.Current.ApplicationInstance.CompleteRequest();
           }
               }
}

my stringWriter seems have value in it i see it while im debuging copy and paste it save as html and it is well formated but why nothing happens ?

what happening wrong? i also tried using Response... but it gives me error on Response.End




Aucun commentaire:

Enregistrer un commentaire