Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
Please I need a fast and best way to export to excel in Visual Basic.net 2010,
I used this one
http://stackoverflow.com/questions/680199/how-to-export-datagridview-to-excel-using-vb-net[^]
but it is very slow.

Thanks.
Posted

VB
' HERE IS THE BEST METOD

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DATAGRIDVIEW_TO_EXCEL(DataGridView1) ' THIS PARAMETER IS YOUR DATAGRIDVIEW
    End Sub

    Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
        Try
            Dim DTB = New DataTable, RWS As Integer, CLS As Integer

            For CLS = 0 To DGV.ColumnCount - 1
                DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
            Next

            Dim DRW As DataRow

            For RWS = 0 To DGV.Rows.Count - 1
                DRW = DTB.NewRow

                For CLS = 0 To DGV.ColumnCount - 1
                    Try
                        DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
                    Catch ex As Exception

                    End Try
                Next

                DTB.Rows.Add(DRW)
            Next

            DTB.AcceptChanges()

            Dim DST As New DataSet
            DST.Tables.Add(DTB)
            Dim FLE As String = "C:\MMS FILES\RESOURCES\XML.xml"                          ' CARPETA Y ARCHIVO DONDE SERÁ CREADO EL XML
            DTB.WriteXml(FLE)
            Dim EXL As String = GetSetting("MasterProduction", "Menu", "txtRutas1", "")   ' EXL = RUTA / Y ARCHIVO DEL EJECUTABLE DE EXCEL (EXCEL.EXE)
            Shell(Chr(34) & EXL & Chr(34) & " " & Chr(34) & FLE & Chr(34), vbNormalFocus) ' ABRIR EXCEL

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
 
Share this answer
 
v2
Comments
Nelek 4-Oct-13 19:07pm    
First of all... do you realize that the question is older than a year?
Second... please get used to tag the snippets with the "code" language that fits
 
Share this answer
 
Comments
Eman Ayad 7-Jun-12 2:54am    
Thank you but it for ASP.
Try this CP article
Export to Excel using VB.Net[^]
 
Share this answer
 
Comments
Eman Ayad 4-Apr-12 5:31am    
Do you know I saw that code before but I put it with the with statement so it didn't work, Now you've guided me to it, now I used it without with statement and it works successfully,
Thank you very much.
 
Share this answer
 
Comments
Eman Ayad 4-Apr-12 4:16am    
Thank you I saw it before but it is in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900