Tuesday, September 7, 2010

Export Document in ASP.NET

Here we are exporting the data from a grid to word,

       Response.Clear();
       Response.Buffer = true;
       Response.AddHeader("Content-Disposition", "attachment;filename=Test.doc");
       Response.ContentType = "application/ms-word";
       StringWriter stringWriter = new StringWriter();
       HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
       gvwEmployeeGrid.RenderControl(htmlTextWriter);
       Response.Output.Write(stringWriter.ToString());
       Response.Flush();
       Response.End();



Make EventValidation to false for the page and also override VerifyRenderingInServerForm in code behind,

       public override void VerifyRenderingInServerForm(Control control)

       {

       }


Export a file in ASP.NET

Here we are exporting a file to the client this will ask a open save cancel dialog

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", "attachment;filename=Test.doc");
    Response.ContentType = "application/ms-Excel";
    Response.WriteFile("New Microsoft Office Word Document.docx");
    Response.Flush();
    Response.End();

0 comments:

Post a Comment