How to export dataGridView data Instantly to Excel on button click?

前端 未结 14 2252
执笔经年
执笔经年 2020-11-28 05:21

I have 10k rows and 15 column in my data grid view. I want to export this data to an excel sheet o button click. I have already tried with the below code.

pr         


        
14条回答
  •  余生分开走
    2020-11-28 05:30

    alternatively you can perform a fast export without using Office dll, as Excel can parse csv files without problems.

    Doing something like this (for less than 65.536 rows with titles):

      Try
    
                If (p_oGrid.RowCount = 0) Then
                    MsgBox("No data", MsgBoxStyle.Information, "App")
                    Exit Sub
                End If
    
                Cursor.Current = Cursors.WaitCursor
    
                Dim sText As New System.Text.StringBuilder
                Dim sTmp As String
                Dim aVisibleData As New List(Of String)
    
                For iAuxRow As Integer = 0 To p_oGrid.Columns.Count - 1
                    If p_oGrid.Columns(iAuxRow).Visible Then
                        aVisibleData.Add(p_oGrid.Columns(iAuxRow).Name)
                        sText.Append(p_oGrid.Columns(iAuxRow).HeaderText.ToUpper)
                        sText.Append(";")
                    End If
                Next
                sText.AppendLine()
    
                For iAuxRow As Integer = 0 To p_oGrid.RowCount - 1
                    Dim oRow As DataGridViewRow = p_oGrid.Rows(iAuxRow)
                    For Each sCol As String In aVisibleData
                        Dim sVal As String
                        sVal = oRow.Cells(sCol).Value.ToString()
                        sText.Append(sVal.Replace(";", ",").Replace(vbCrLf, " ; "))
                        sText.Append(";")
                    Next
                    sText.AppendLine()
                Next
    
                sTmp = IO.Path.GetTempFileName & ".csv"
                IO.File.WriteAllText(sTmp, sText.ToString, System.Text.Encoding.UTF8)
                sText = Nothing
    
                Process.Start(sTmp)
    
            Catch ex As Exception
                process_error(ex)
            Finally
                Cursor.Current = Cursors.Default
            End Try
    

提交回复
热议问题