Tuesday, March 27, 2012

Exporting an ASP.NET gridview to PDF and CSV formats

You will need to download and add a reference to the iTextSharp dll in your ASP.NET application.
Here query1Gridview is the Gridview that I want to export to PDF. The assumption is that the gridview is already provided the datasource before you're ready to export.


Response.ContentType = "application/pdf"

        Response.AddHeader("content-disposition", _
      "attachment;filename=YOURFILENAME.pdf")
        Dim m As New MemoryStream
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        query1GridView.AllowPaging = False
        query1GridView.DataBind()
        query1GridView.RenderControl(hw)
        Dim dtNow As DateTime
        dtNow = Now()
        Dim todaysDate = dtNow.ToLongDateString
        Dim sr As New StringReader(sw.ToString())

        Dim header As String
        header = "YOUR HEADER" + Environment.NewLine
        Dim c As Chunk = New Chunk(header & vbLf, FontFactory.GetFont("Verdana", 15))
        Dim p As New Paragraph()
        p.Alignment = Element.ALIGN_CENTER
        p.Add(c)

        Dim c1 As Chunk = New Chunk("ADD YOUR QUERY TEXT" + Environment.NewLine & vbLf, FontFactory.GetFont("Verdana", 15))
        Dim p1 As New Paragraph()
        p1.Alignment = Element.ALIGN_CENTER
        p1.Add(c1)

        Dim c2 As Chunk = New Chunk(todaysDate + Environment.NewLine & vbLf, FontFactory.GetFont("Times New Roman", 12))
        Dim p2 As New Paragraph()
        p2.Alignment = Element.ALIGN_RIGHT
        p2.Add(c2)


        Dim logo As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~\Images\YOURLOGO.jpg"))
        logo.Alignment = Element.ALIGN_LEFT
        logo.Alignment = iTextSharp.text.Image.TEXTWRAP
        logo.ScaleAbsoluteHeight(60)
        logo.ScaleAbsoluteWidth(250)

        Dim pdfDoc As New Document(PageSize.A3, 10.0F, 10.0F, 10.0F, 0.0F)
        Try
            Dim htmlparser As New HTMLWorker(pdfDoc)
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
            pdfDoc.Open()
            pdfDoc.Add(logo)
            pdfDoc.Add(p)
            pdfDoc.Add(p1)
            pdfDoc.Add(p2)
            htmlparser.Parse(sr)
            pdfDoc.Close()
            Response.Write(pdfDoc)
            Response.End()
        Catch ex As Exception
            MessageBox("Unable to create pdf")
        End Try


For exporting to CSV use the below code:


Response.Clear()

        Response.Buffer = True

        Response.AddHeader("content-disposition", "attachment;filename=YOURFILENAME.csv")
        Response.Charset = ""
        Response.ContentType = "application/text"
        query1GridView.AllowPaging = False

        query1GridView.DataBind()
        Dim sb As New StringBuilder()

        For k As Integer = 0 To query1GridView.Columns.Count - 1

            sb.Append(query1GridView.Columns(k).HeaderText + ","c)

        Next
        sb.Append(vbCr & vbLf)
        For i As Integer = 0 To query1GridView.Rows.Count - 1

            For k As Integer = 0 To query1GridView.Columns.Count - 1

                'add separator

                sb.Append(query1GridView.Rows(i).Cells(k).Text + ","c)

            Next
            sb.Append(vbCr & vbLf)

        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()

No comments:

Post a Comment

Your comments are moderated by your ISP.