borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (how to display images in datagrid from database)Next Topic (how to encrypt forms data ?) New Topic New Poll Post Reply
AndreaVB Forum : ASP.Net : how to export the content of datagrid to excel file in asp.net
Poster Message
tri_inn
Level: Regular User
Registered: 26-08-2002
Posts: 395

icon how to export the content of datagrid to excel file in asp.net

i want to design my form in a such a way that there will be one datagrid with records and one button. when user will click on that button then the content
of the grid will be exported in a excel file and that file will be saved in client machine to any directory according user choice but i don't want to
solve it taking and working with excel reference.please give me easy code for solving my purpose.

14-06-2004 at 12:39 PM
View Profile Send Email to User Show All Posts | Quote Reply
zimcoder
Level: VB Lord


Registered: 27-10-2003
Posts: 225
icon Re: how to export the content of datagrid to excel file in asp.net

You can Export the data into CSV format that will allow excel to read it as an excel document. The method below
Get the data from the datasource of the datagrid rather than from the datagrid itself

Private Sub btnexport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexport.Click
Dim sw As StreamWriter = New StreamWriter(ConfigurationSettings.AppSettings("reportspath") & "\myexcel.csv", False)
        Dim dataset As DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim i, j As Integer

       'Use a function to get data from datasource and bind to 'datagrid-- GetData()
        dataset =GetData()
        dt = dataset.Tables(0)
        Dim iColCount As Integer = dt.Columns.Count
        sw.WriteLine("My excel output")
        sw.WriteLine()
' Writing the headers.
        For i = 0 To iColCount - 1

            sw.Write(dt.Columns(i))
            If i < iColCount - 1 Then

                sw.Write(",")
            End If
        Next
        sw.Write(sw.NewLine)
        ' Now write all the rows.
        For Each dr In dt.Rows

            For j = 0 To iColCount - 1

                If Not Convert.IsDBNull(dr(j)) Then

                    sw.Write(dr(j).ToString())
                End If
                If j < iColCount - 1 Then

                    sw.Write(",")
                End If
            Next
            sw.Write(sw.NewLine)


        Next

        sw.Close()End Sub  

     Give the appropriate confirmation and you are done!
  

____________________________
BrainBench ADO.NET and ASP.NET Certified Developer

27-07-2004 at 09:14 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : ASP.Net : how to export the content of datagrid to excel file in asp.net
Previous Topic (how to display images in datagrid from database)Next Topic (how to encrypt forms data ?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder