How to export a datagrid values in PDF
Hi,I would like to export the records form datagrid to a PDF file.googled for many samples, blogs but no help. Can anyone help me with that.
Thanks in advance,
nitz.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
String strCon = System.Configuration.ConfigurationManager
.ConnectionStrings["EmpDBConnectionSring"].ConnectionString;
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strSQL = "SELECT * FROM Emptab";
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
da.Fill(ds, "test");
GvEmp.DataSource = ds.Tables[0];
GvEmp.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=EmployeeDetails.pdf");
Response.Charset = "";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GvEmp.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim ds As New DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' ShowGridview()
End If
End Sub
Sub ShowGridview()
'bind the database values to Gridveiw using dataset control
Dim con As SqlClient.SqlConnection
Dim da As SqlDataAdapter
Dim str As String
str = "USER=UserID;PASSWORD=PASSWORD;SERVER=SERVER-Name;DATABASE=DATABASE-Name"
con = New SqlConnection(str)
da = New SqlDataAdapter("select FieldName1,FieldName2 from Table_Name", con)
da.Fill(ds, "Employee")
ViewState("DataSetValue") = ds
grvGrid.DataSource = ds
grvGrid.DataBind()
con.Close()
End Sub
Private Sub ConvertGridviewToPdfDocument(ByVal dstHeader2 As DataSet)
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\3.pdf", FileMode.Create))
doc.Open()
Dim table As New PdfPTable(dstHeader2.Tables(0).Columns.Count)
Dim widths As Integer() = {25, 25}
table.WidthPercentage = "100"
For i As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnHeader As New Paragraph(dstHeader2.Tables(0).Columns(i).ColumnName.ToString(), FontFactory.GetFont("verdana", 11))
ColumnHeader.Font.SetStyle(Font.BOLD)
Dim cell As New PdfPCell(New Phrase(ColumnHeader))
cell.HorizontalAlignment = Element.ALIGN_CENTER
table.AddCell(cell)
Next
For k As Integer = 0 To dstHeader2.Tables(0).Rows.Count - 1
For j As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnValue As New Paragraph(dstHeader2.Tables(0).Rows(k)(j).ToString(), FontFactory.GetFont("verdana", 10))
table.AddCell(ColumnValue)
Next
Next
table.SetWidths(widths)
doc.Add(table)
doc.Add(New Paragraph(vbLf))
doc.Close()
Response.Redirect("~/3.pdf")
End Sub
Protected Sub btnConvertGridviewToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertGridviewToPDF.Click
'ConvertGridviewToPdfDocument(ViewState("DataSetValue"))
Dim str As String = ""
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(FileUpload1.PostedFile.FileName, FileMode.Create))
'string strFileNameWithPath = FileUpload1.PostedFile.FileName;
doc.Open()
doc.Close()
End Sub
End Class
Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!