In SQL Server, first we create one table, tbl_EmpDetails.
- CREATE TABLE [dbo].[tbl_EmpDetails](
- [EmpID] [int] IDENTITY(1,1) NOT NULL,
- [EmpName] [varchar](100) NOT NULL,
- [EmpAddress] [nvarchar](100) NOT NULL,
- [Mobile] [varchar](20) NOT NULL,
- [EmailID] [nvarchar](50) NOT NULL,
- [DOB] [varchar](50) NOT NULL,
- CONSTRAINT [PK_tbl_EmpDetails] PRIMARY KEY CLUSTERED
- (
- [EmpID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
Insert Employee details in Table.
- INSERT INTO tbl_EmpDetails Values('Anil','Mumbai',1233456789,'[email protected]','1989-10-10')
- INSERT INTO tbl_EmpDetails Values('Suresh','Mumbai',1233456789,'[email protected]','1989-10-10')
- INSERT INTO tbl_EmpDetails Values('Ramesh','Mumbai',1233456789,'[email protected]','1989-10-10')
Now, we create ASP.Net Web Application.
Add below namespace,
- using System.Text;
- using System.Data.SqlClient;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
- using System.Data;
- using System.Configuration;
- using System.IO;
On page load we bind grid as below code .
- public partial class EmployeeList : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- BindGrid();
- }
- }
In BindGrid function we get data from database and store value in session.
- protected void BindGrid() {
- DataTable dt = new DataTable();
- string queryString = "select * from tbl_EmpDetails";
- string conn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString();
- var table = new DataTable();
- using(SqlConnection sql = new SqlConnection(conn)) {
- SqlCommand command = new SqlCommand(queryString, sql);
- sql.Open();
- SqlDataAdapter da = new SqlDataAdapter(command);
- da.Fill(table);
- sql.Close();
- da.Dispose();
- }
- dt = table;
- GridView1.DataSource = dt;
- GridView1.DataBind();
- Session["Data"] = dt;
- }
Now run program, you will have the below output.
Export to PDF Button Click Code,
- protected void ExportPDf_Click(object sender, EventArgs e) {
- try {
- string Err = string.Empty;
- DataTable dt = (DataTable) Session["Data"];
- Document pdfDocument = new Document();
- StringBuilder sb = new StringBuilder();
-
- if (dt.Rows.Count > 0) {
- sb.Append("<html>< head ><title></title></head><body>");
- string strActualRecords = string.Empty;
- strActualRecords = "<table style=\"width:100%;font-size: 9pt; font-family: verdana; \" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
- strActualRecords += "<tr><td style=\"width: 5%;white-space: nowrap;\">EMPID</td><td style=\"width: 5%;\">EmpName</td><td style=\"width: 5%;\">EmpAddress</td>" + "<td style=\"width: 5%;\">Mobile</td>" + "<td style=\"width: 5%;\">EmailID</td>" + "< td style =\"width: 5%;\">DOB</td></tr>";
- for (int i = 0; i < dt.Rows.Count; i++) {
- strActualRecords += "<tr>";
- strActualRecords += "<td style=\"width: 5%;\">" + dt.Rows[i]["EMPID"].ToString() + "</td>" + "<td style=\"width: 5%;\">" + dt.Rows[i]["EmpName"].ToString() + "</td>" + "<td style=\"width: 5%;\">" + dt.Rows[i]["EmpAddress"].ToString() + "</td>" + "<td style=\"width: 5%;\">" + dt.Rows[i]["Mobile"].ToString() + "</td>" + "<td style=\"width: 5%;\">" + dt.Rows[i]["EmailID"].ToString() + "</td>" + "<td style=\"width: 5%;\">" + dt.Rows[i]["DOB"].ToString() + "</td>";
- strActualRecords += "</tr>";
- }
- strActualRecords += "</table>";
- sb.Append(strActualRecords);
- sb.Append("</body></html>");
- }
- string pdffilename = DateTime.Now.Ticks.ToString() + ".pdf";
- PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);
- pdfDocument.Open();
- String htmlText = sb.ToString();
- StringReader str = new StringReader(htmlText);
- HTMLWorker htmlworker = new HTMLWorker(pdfDocument);
- htmlworker.Parse(str);
- pdfWriter.CloseStream = false;
- pdfDocument.Close();
-
- Response.Buffer = true;
- Response.ContentType = "application/pdf";
- Response.AppendHeader("Content-Disposition", "attachment; filename=" + pdffilename);
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.Write(pdfDocument);
- Response.Flush();
- Response.End();
- } catch (Exception ex) {
- throw ex;
- }
- }
Code Description
DataTable dt = (DataTable)Session["Data"];
In this line we assign Session stored value to Datatable.
Then create StringBuilder object and append value of datatable dt using For loops.
Below code is used to read, write Content in pdf and download PDF content.
Output