locked
GRIDVIEW PDF/EXCEL RRS feed

  • Question

  • How to export GridView data to PDF and Excel data in asp.net
    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Friday, October 7, 2011 10:05 AM

Answers

  • Hi,

    Take a look at the following links

    http://forums.asp.net/t/1412788.aspx

    http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

    http://www.dotnetspider.com/resources/42818-How-export-Grid-View-Data-PDF-or-Excel.aspx

    Regards,
    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Friday, October 7, 2011 10:26 AM
  • Grid View Export To Excle
    ----------
                Dim strAttahment As String = "attachment; filename=SaudaSoftClientList_" & System.DateTime.Now.ToString("ddMMMyyyy") & ".xls"
                Response.ClearContent()
                Response.AddHeader("content-disposition", strAttahment)
                Response.ContentType = "application/ms-excel"
                Dim strWriter As New StringWriter()
                Dim htmlTxtWriter As New HtmlTextWriter(strWriter)
                Dim htmlPage As New HtmlForm()
                gvExportToExcle.Parent.Controls.Add(htmlPage)
                htmlPage.Attributes("runat") = "server"
                htmlPage.Controls.Add(gvExportToExcle)
                htmlPage.RenderControl(htmlTxtWriter)
                Response.Write(strWriter.ToString())
                Response.End()
    ----------------------------------------------------------------------------
    Grid View Export To Pdf
    ------------------
    Download itextsharp.dll
    -----------
    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    ---------------
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=ibook.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    gvdetails.AllowPaging = False
    gvdetails.DataBind()
    gvdetails.RenderControl(hw)
    gvdetails.HeaderRow.Style.Add("width", "15%")
    gvdetails.HeaderRow.Style.Add("font-size", "10px")
    gvdetails.Style.Add("text-decoration", "none")
    gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;")
    gvdetails.Style.Add("font-size", "8px")
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A2, 7F, 7F, 7F, 0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()

    Kamal Saxena iBOOK dESK (UITS INDIA)
    Saturday, October 8, 2011 10:27 AM
  • Hi,

     

    Welcome to the MSDN forum.

     

    According to your description, I'd like to move your thread to off-topic because it is a problem related to ASP.NET. You may post thread in ASP.NET Forum. There are more experts in that forum so that you may get more useful help there.

     

    Thank you for your posting and understanding.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 25, 2011 3:14 AM
  • see my DataGridViewExportSample at my Skydrive to how to export data from a table!

    Finally, you need a PDF Library like PDFSharp or iTextSharp to export your data to PDF and need a CSV Writer(Part I && Part II) or Excel Component like Spire.XLS to export your data into excel sheets!


    Any fool can know. The point is to understand.(Albert Einstein)
    Friday, October 7, 2011 10:35 AM
  • Thank U
    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Monday, October 10, 2011 6:47 AM
  • i am having a problem in the export to CVS i am getting whole aspx page instead of gridview

    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Monday, October 10, 2011 8:46 AM
  • please share your source code!
    Any fool can know. The point is to understand.(Albert Einstein)
    Monday, October 10, 2011 1:14 PM
  • <asp:GridView ID="GridView1" runat="server" CssClass="GvwCtl" AutoGenerateDeleteButton="True"
                AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="EmpID"
                DataSourceID="MSIEmpDSID" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True"
                CellPadding="4" ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <PagerSettings Mode="Numeric" Position="Bottom" PageButtonCount="10" />
                <PagerStyle BackColor="Red" />
                <Columns>
                    <asp:BoundField DataField="EmpID" HeaderText="EmpID" ReadOnly="True" SortExpression="EmpID" />
                    <asp:BoundField DataField="EName" HeaderText="EName" SortExpression="EName" />
                    <asp:BoundField DataField="EDiskNO" HeaderText="EDiskNO" SortExpression="EDiskNO" />
                    <asp:BoundField DataField="ESalary" HeaderText="ESalary" SortExpression="ESalary" />
                    <asp:BoundField DataField="ELastName" HeaderText="ELastName" SortExpression="ELastName" />
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
       
            <asp:SqlDataSource ID="MSIEmpDSID" runat="server" ConnectionString="<%$ ConnectionStrings:EFDBConnectionString %>"
                SelectCommand="SELECT [EmpID], [EName], [EDiskNO], [ESalary], [ELastName] FROM [MSIEmp]">
            </asp:SqlDataSource>
            <asp:Button ID="btnExportToWord" runat="server" Text="Word" />
            <asp:Button ID="btnExportToExcel" runat="server" Text="Excel" />
            <asp:Button ID="btnExportToPDF" runat="server" Text="PDF" />


    <asp:Button ID="btnExportToCSV" runat="server" Text="CSV" />    


    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision

    Monday, October 10, 2011 1:17 PM

All replies

  • Hi,

    Take a look at the following links

    http://forums.asp.net/t/1412788.aspx

    http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

    http://www.dotnetspider.com/resources/42818-How-export-Grid-View-Data-PDF-or-Excel.aspx

    Regards,
    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Friday, October 7, 2011 10:26 AM
  • see my DataGridViewExportSample at my Skydrive to how to export data from a table!

    Finally, you need a PDF Library like PDFSharp or iTextSharp to export your data to PDF and need a CSV Writer(Part I && Part II) or Excel Component like Spire.XLS to export your data into excel sheets!


    Any fool can know. The point is to understand.(Albert Einstein)
    Friday, October 7, 2011 10:35 AM
  • Grid View Export To Excle
    ----------
                Dim strAttahment As String = "attachment; filename=SaudaSoftClientList_" & System.DateTime.Now.ToString("ddMMMyyyy") & ".xls"
                Response.ClearContent()
                Response.AddHeader("content-disposition", strAttahment)
                Response.ContentType = "application/ms-excel"
                Dim strWriter As New StringWriter()
                Dim htmlTxtWriter As New HtmlTextWriter(strWriter)
                Dim htmlPage As New HtmlForm()
                gvExportToExcle.Parent.Controls.Add(htmlPage)
                htmlPage.Attributes("runat") = "server"
                htmlPage.Controls.Add(gvExportToExcle)
                htmlPage.RenderControl(htmlTxtWriter)
                Response.Write(strWriter.ToString())
                Response.End()
    ----------------------------------------------------------------------------
    Grid View Export To Pdf
    ------------------
    Download itextsharp.dll
    -----------
    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    ---------------
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=ibook.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    gvdetails.AllowPaging = False
    gvdetails.DataBind()
    gvdetails.RenderControl(hw)
    gvdetails.HeaderRow.Style.Add("width", "15%")
    gvdetails.HeaderRow.Style.Add("font-size", "10px")
    gvdetails.Style.Add("text-decoration", "none")
    gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;")
    gvdetails.Style.Add("font-size", "8px")
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A2, 7F, 7F, 7F, 0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()

    Kamal Saxena iBOOK dESK (UITS INDIA)
    Saturday, October 8, 2011 10:27 AM
  • Thank U
    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Monday, October 10, 2011 6:47 AM
  • i am having a problem in the export to CVS i am getting whole aspx page instead of gridview

    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Monday, October 10, 2011 8:46 AM
  • please share your source code!
    Any fool can know. The point is to understand.(Albert Einstein)
    Monday, October 10, 2011 1:14 PM
  • <asp:GridView ID="GridView1" runat="server" CssClass="GvwCtl" AutoGenerateDeleteButton="True"
                AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="EmpID"
                DataSourceID="MSIEmpDSID" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True"
                CellPadding="4" ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <PagerSettings Mode="Numeric" Position="Bottom" PageButtonCount="10" />
                <PagerStyle BackColor="Red" />
                <Columns>
                    <asp:BoundField DataField="EmpID" HeaderText="EmpID" ReadOnly="True" SortExpression="EmpID" />
                    <asp:BoundField DataField="EName" HeaderText="EName" SortExpression="EName" />
                    <asp:BoundField DataField="EDiskNO" HeaderText="EDiskNO" SortExpression="EDiskNO" />
                    <asp:BoundField DataField="ESalary" HeaderText="ESalary" SortExpression="ESalary" />
                    <asp:BoundField DataField="ELastName" HeaderText="ELastName" SortExpression="ELastName" />
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
       
            <asp:SqlDataSource ID="MSIEmpDSID" runat="server" ConnectionString="<%$ ConnectionStrings:EFDBConnectionString %>"
                SelectCommand="SELECT [EmpID], [EName], [EDiskNO], [ESalary], [ELastName] FROM [MSIEmp]">
            </asp:SqlDataSource>
            <asp:Button ID="btnExportToWord" runat="server" Text="Word" />
            <asp:Button ID="btnExportToExcel" runat="server" Text="Excel" />
            <asp:Button ID="btnExportToPDF" runat="server" Text="PDF" />


    <asp:Button ID="btnExportToCSV" runat="server" Text="CSV" />    


    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision

    Monday, October 10, 2011 1:17 PM
  • Protected Sub btnExportCSV_Click(ByVal sender As Object,ByVal e As EventArgs)

     Response.Clear()

     Response.Buffer = True

     Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.csv")

     Response.Charset = ""

     Response.ContentType = "application/text"

     

     GridView1.AllowPaging = False

     GridView1.DataBind()

     

     Dim sb As New StringBuilder()

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

      'add separator

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

     Next

     'append new line

     sb.Append(vbCr & vbLf)

     For i As Integer = 0 To GridView1.Rows.Count - 1

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

       'add separator

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

      Next

      'append new line

      sb.Append(vbCr & vbLf)

     Next

     Response.Output.Write(sb.ToString())

     Response.Flush()

     Response.End()

    End Sub
    Thanks & Regards, Naveen Kumar Kacham Media Systems Mobile: +91 999.521.6653 www.mediaus.com Powered by Passion, Steered by Vision
    Monday, October 10, 2011 1:19 PM
  • Hi,

     

    Welcome to the MSDN forum.

     

    According to your description, I'd like to move your thread to off-topic because it is a problem related to ASP.NET. You may post thread in ASP.NET Forum. There are more experts in that forum so that you may get more useful help there.

     

    Thank you for your posting and understanding.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 25, 2011 3:14 AM