DataGridView nach Excel exportieren

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Tascapone.

    DataGridView nach Excel exportieren

    Guten Tag alle zusammen,

    ich bin gerade dabei ein Code zu schreiben der 3 große DataGridViews (ca. 3300 Zeilen x 9 Spalten , Integer/DateTime) nach Excel exportieren soll. Dazu führe ich die GridView in ein DataSet und von dort nach Excel. Allerdings dauert mir das zu lange ca. 5 Minuten. 1. Frage : Gibt es eine schnellere Variante oder seht ihr eine Leistungsschwäche im folgenden Code?
    2. Frage: Kann man auch ChartSeries "Abbildungen" nach Excel exportieren und wie würde es funktionieren?

    Visual Basic-Quellcode

    1. [/b] Dim regDate As Date = Date.Now()
    2. Dim strDate As String = regDate.ToString("MM-dd-yyyy")
    3. Dim saveFileDialog1 As New SaveFileDialog()
    4. saveFileDialog1.Filter = "Excel Arbeitsmappe (.xls)|*.xls"
    5. saveFileDialog1.Title = "Als Excel Datei exportieren"
    6. saveFileDialog1.FileName = "exportedxx-" & strDate & ".xls"
    7. If saveFileDialog1.FileName IsNot "" And saveFileDialog1.ShowDialog = DialogResult.OK = True Then
    8. If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
    9. Exit Sub
    10. End If
    11. System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
    12. Dim dset As New DataSet
    13. 'add table to dataset
    14. dset.Tables.Add()
    15. 'add column to that table
    16. For i As Integer = 0 To DataGridView1.ColumnCount - 1
    17. dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
    18. Next
    19. 'add rows to the table
    20. Dim dr1 As DataRow
    21. For i As Integer = 0 To DataGridView1.RowCount - 1
    22. dr1 = dset.Tables(0).NewRow
    23. For j As Integer = 0 To DataGridView1.Columns.Count - 1
    24. dr1(j) = DataGridView1.Rows(i).Cells(j).Value
    25. Next
    26. dset.Tables(0).Rows.Add(dr1)
    27. Next
    28. 'add table to dataset
    29. dset.Tables.Add()
    30. 'add column to that table
    31. For i As Integer = 0 To DataGridView2.ColumnCount - 1
    32. dset.Tables(1).Columns.Add(DataGridView2.Columns(i).HeaderText)
    33. Next
    34. 'add rows to the table
    35. Dim dr2 As DataRow
    36. For i As Integer = 0 To DataGridView2.RowCount - 1
    37. dr2 = dset.Tables(1).NewRow
    38. For j As Integer = 0 To DataGridView2.Columns.Count - 1
    39. dr2(j) = DataGridView2.Rows(i).Cells(j).Value
    40. Next
    41. dset.Tables(1).Rows.Add(dr2)
    42. Next
    43. dset.Tables.Add()
    44. For i As Integer = 0 To DataGridView3.ColumnCount - 1
    45. dset.Tables(2).Columns.Add(DataGridView3.Columns(i).HeaderText)
    46. Next
    47. 'add rows to the table
    48. Dim dr3 As DataRow
    49. For i As Integer = 0 To DataGridView3.RowCount - 1
    50. dr3 = dset.Tables(2).NewRow
    51. For j As Integer = 0 To DataGridView3.Columns.Count - 1
    52. dr3(j) = DataGridView3.Rows(i).Cells(j).Value
    53. Next
    54. dset.Tables(2).Rows.Add(dr3)
    55. Next
    56. Dim excel As New Microsoft.Office.Interop.Excel.Application
    57. Dim wBook As Microsoft.Office.Interop.Excel.Workbook
    58. Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
    59. Dim wSheet2 As Microsoft.Office.Interop.Excel.Worksheet
    60. Dim wSheet3 As Microsoft.Office.Interop.Excel.Worksheet
    61. 'excel.Workbooks.Close()
    62. wBook = excel.Workbooks.Add()
    63. wSheet = wBook.ActiveSheet()
    64. wSheet.Name = "xxx"
    65. Dim dt As System.Data.DataTable = dset.Tables(0)
    66. Dim dt2 As System.Data.DataTable = dset.Tables(1)
    67. Dim dt3 As System.Data.DataTable = dset.Tables(2)
    68. Dim dc As System.Data.DataColumn
    69. Dim dr As System.Data.DataRow
    70. Dim colIndex As Integer = 0
    71. Dim rowIndex As Integer = 0
    72. For Each dc In dt.Columns
    73. colIndex = colIndex + 1
    74. excel.Cells(1, colIndex) = dc.ColumnName
    75. excel.Cells(1, colIndex).Interior.ColorIndex = 23
    76. excel.Cells(1, colIndex).Font.Name = "Arial"
    77. excel.Cells(1, colIndex).Font.Size = 10
    78. Next
    79. For Each dr In dt.Rows
    80. rowIndex = rowIndex + 1
    81. colIndex = 0
    82. For Each dc In dt.Columns
    83. colIndex = colIndex + 1
    84. excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    85. excel.Cells(rowIndex + 1, colIndex).EntireRow.Borders.ColorIndex = 1
    86. Next
    87. Next
    88. wSheet.Columns.AutoFit()
    89. wSheet2 = wBook.Sheets.Add()
    90. wSheet2 = wBook.ActiveSheet()
    91. wSheet2.Name = "xxx"
    92. colIndex = 0
    93. rowIndex = 0
    94. For Each dc In dt2.Columns
    95. colIndex = colIndex + 1
    96. excel.Cells(1, colIndex) = dc.ColumnName
    97. excel.Cells(1, colIndex).Interior.ColorIndex = 23
    98. excel.Cells(1, colIndex).Font.Name = "Arial"
    99. excel.Cells(1, colIndex).Font.Size = 10
    100. Next
    101. For Each dr In dt2.Rows
    102. rowIndex = rowIndex + 1
    103. colIndex = 0
    104. For Each dc In dt2.Columns
    105. colIndex = colIndex + 1
    106. excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    107. excel.Cells(rowIndex + 1, colIndex).Borders.ColorIndex = 1
    108. Next
    109. Next
    110. wSheet2.Columns.AutoFit()
    111. wSheet3 = wBook.Sheets.Add()
    112. wSheet3 = wBook.ActiveSheet()
    113. wSheet3.Name = "xxx"
    114. For Each dc In dt3.Columns
    115. colIndex = colIndex + 1
    116. excel.Cells(1, colIndex) = dc.ColumnName
    117. excel.Cells(1, colIndex).Interior.ColorIndex = 23
    118. excel.Cells(1, colIndex).Font.Name = "Arial"
    119. excel.Cells(1, colIndex).Font.Size = 10
    120. Next
    121. For Each dr In dt3.Rows
    122. rowIndex = rowIndex + 1
    123. colIndex = 0
    124. For Each dc In dt3.Columns
    125. colIndex = colIndex + 1
    126. excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    127. excel.Cells(rowIndex + 1, colIndex).Borders.ColorIndex = 1
    128. Next
    129. Next
    130. Dim strFileName As String = saveFileDialog1.FileName
    131. Dim blnFileOpen As Boolean = False
    132. Try
    133. Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
    134. fileTemp.Close()
    135. Catch ex As Exception
    136. blnFileOpen = False
    137. End Try
    138. If System.IO.File.Exists(strFileName) Then
    139. System.IO.File.Delete(strFileName)
    140. End If
    141. wBook.SaveAs(strFileName)
    142. excel.Workbooks.Open(strFileName)
    143. excel.Visible = True[b]


    Vielen Dank!
    Also der Code ist zweimal 3-fach redundant, aber einen Schlehmil-Algorithmus sehe ich auf die Schnelle nicht.

    Wäre also erstmal festzustellen, wo's überhaupt hapert.
    Und gut möglich, dasses mit Office-InterOp auch garnet schnell geht.

    Die am häufigsten durchlaufenen Zeilen sind ja die inneren For-schleifen, hier zB #6 + #7

    VB.NET-Quellcode

    1. For Each dr In dt3.Rows
    2. rowIndex = rowIndex + 1
    3. colIndex = 0
    4. For Each dc In dt3.Columns
    5. colIndex = colIndex + 1
    6. excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    7. excel.Cells(rowIndex + 1, colIndex).Borders.ColorIndex = 1
    8. Next
    9. Next
    Kommentiere da mal die eine mal die andere mal beide aus, und vergleiche die Zeiten.
    Und das natürlich immer in allen 3 Tabellen gleichzeitig machen.
    Vielen Dank für die Antwort!

    Habe jetzt wie beschrieben einige Zeilen auskommentiert. Die innerste Schleife dauert wie gesagt am längsten, doch wenn ich die Zeile 7 auskommentiere (Bordes.ColorIndex) komme ich auf 40 - 60 s.

    Ich komme auf den Entschluss das Formatierung über vb gesteuert sehr leistungsmindernd sind (hier: 4min X/ ).

    Hast du vielleicht eine Ahnung zu Frage2 ?
    Ich habe in meiner Form ein Diagramm erstellt (In der ToolBox als "Chart" aufgelistet) diese möchte ich beim exportieren auch in einer Excel Mappe darstellen.
    Den Vorgang zum Abspeichern des Charts als Bilddatei habe ich hinbekommen mit

    Visual Basic-Quellcode

    1. Chart1.SaveImage(VollerName, ChartImageFormat.Jpeg)
    . Jetzt fehlt lediglich der Export nach Excel