Tuesday, March 03, 2015

Convert GridView To Document or Excel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
   SqlConnection con = new SqlConnection("Data Source=AAPTI-A20B83733\\SQLEXPRESS;Initial Catalog=NewLogin;Integrated Security=True");
   protected void Page_Load(object sender, EventArgs e)
   {
       SqlDataAdapter da = new SqlDataAdapter("select * from LoginDetails", con);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView1.DataSource = ds;
       GridView1.DataBind();
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       
       string fileName = "attachment;filename= ExampleDOC.doc";
       Response.ClearContent();
       Response.AddHeader("content-disposition", fileName);
       Response.ContentType = "application/doc";

       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);

       GridView1.RenderControl(hw);
       Response.Write(sw.ToString());
       Response.End();
   }
   public override void VerifyRenderingInServerForm(Control control)
   {
      
   }
}

No comments:

Post a Comment