Saturday, June 1, 2013

How to Import data from grid to pdf and excel in VB.Net

 Import data from grid to pdf :To import data from grid to pdf we can use third part control namely "iTextSharp" so we have to download this "iTextSharp.dll" and add to to your project

http://sourceforge.net/projects/itextsharp/  you can download the iTextSharp.dll from this link

Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Imports iTextSharp.text.html.simpleparser

Protected Sub imgexportpdf_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgexportpdf.Click
        Using sw As New StringWriter()
            Using hw As New HtmlTextWriter(sw)
                'To Export all pages
                gvCustomer.AllowPaging = False
                BindCustomerDetails()

                gvCustomer.RenderControl(hw)
                Dim sr As New StringReader(sw.ToString())
                Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
                Dim htmlparser As New HTMLWorker(pdfDoc)
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
                pdfDoc.Open()
                htmlparser.Parse(sr)
                pdfDoc.Close()

                Response.ContentType = "application/pdf"
                Response.AddHeader("content-disposition",     "attachment;filename=GridViewExport.pdf")
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.Write(pdfDoc)
                Response.[End]()
            End Using
        End Using
    End Sub


Import data from grid to Excel:

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing

Imports System.Data.OleDb


 Protected Sub imgexportexcel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgexportexcel.Click
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=CustomerDetails.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Using sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            'To Export all pages
            gvCustomer.AllowPaging = False
            BindCustomerDetails()

            gvCustomer.HeaderRow.BackColor = Drawing.Color.GreenYellow

            For Each cell As TableCell In gvCustomer.HeaderRow.Cells
                cell.BackColor = Drawing.Color.GreenYellow
            Next
            For Each row As GridViewRow In gvCustomer.Rows
                row.BackColor = Drawing.Color.White
                For Each cell As TableCell In row.Cells
                    If row.RowIndex Mod 2 = 0 Then
                        cell.BackColor = gvCustomer.AlternatingRowStyle.BackColor
                    Else
                        cell.BackColor = gvCustomer.RowStyle.BackColor
                    End If
                    cell.CssClass = "textmode"
                Next
            Next

            gvCustomer.RenderControl(hw)
            'style to format numbers to string
            Dim style As String = "<style> .textmode { } </style>"
            Response.Write(style)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.[End]()
        End Using

    End Sub

No comments:

Post a Comment