Export Data from Sql 05 to Word,Excel and Pdf

Dotnet4ashu
Posted by Dotnet4ashu under ASP.NET category on | Points: 40 | Views : 1718
hello friend this is one of the simplest code for export data from sql to word, excel and pdf. I have took gridview control to making this code. I hope this would help you.

you have to use <b>itextsharp.dll</b> in this code

Use these following <b>namespaces</b> to use this code

<b>using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;</b>
public partial class Import_Export : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
<b> callgird();</b>
}
}
public void callgird()
{
SqlCommand cmd = new SqlCommand("select * from products", con);
con.Open();
SqlDataAdapter dad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}


public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

<b>For Word</b>
protected void imgbtnword_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
//Response.End();

}
<b>For Excel</b>
protected void imgbtn_Excel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
callgird();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
callgird();
}
<b>For PDF</b>
protected void imgpdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
callgird();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}

}

Comments or Responses

Posted by: T.Saravanan on: 4/2/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

Nice Info. Kindly post your code inside the code tag.

Login to post response