簡體   English   中英

將 DataGridView 單元格粘貼到 Excel 電子表格

[英]Paste DataGridView cells to Excel spreadsheet

我試圖找到一種更快的方法將 DataGridView 單元格的值粘貼到 Excel 電子表格中。 下面的代碼有效,但它在循環每個單元格時真的很慢。 希望有人幫忙加快速度。

提前致謝。

          For i = 0 To PartsDataGridView.RowCount - 1
                If PartsDataGridView.Rows(i).Cells(0).Value = "Positive" Then

                    lr3 = WS.Range("A" & WS.Rows.Count).End(Excel.XlDirection.xlUp).Row + 1

                    With WS
                        .Range("A" & lr3).Value = PartsDataGridView.Rows(i).Cells(1).Value
                        .Range("B" & lr3).Value = PartsDataGridView.Rows(i).Cells(2).Value
                        .Range("C" & lr3).Value = PartsDataGridView.Rows(i).Cells(3).Value
                        .Range("D" & lr3).Value = PartsDataGridView.Rows(i).Cells(4).Value
                        .Range("E" & lr3).Value = PartsDataGridView.Rows(i).Cells(5).Value
                        .Range("F" & lr3).Value = PartsDataGridView.Rows(i).Cells(6).Value
                        .Range("G" & lr3).Value = PartsDataGridView.Rows(i).Cells(7).Value
                        .Range("H" & lr3).Value = PartsDataGridView.Rows(i).Cells(8).Value
                        .Range("I" & lr3).Value = PartsDataGridView.Rows(i).Cells(9).Value
                        .Range("J" & lr3).Value = PartsDataGridView.Rows(i).Cells(10).Value
                        .Range("K" & lr3).Value = PartsDataGridView.Rows(i).Cells(11).Value
                        .Range("L" & lr3).Value = PartsDataGridView.Rows(i).Cells(12).Value
                    End With

                End If
          Next

如果我沒記錯的話,您可以將數組粘貼到 Excel 互操作范圍中。

我使用了這樣的東西:

在這種情況下,數組是數據的二維數組

Worksheet.Range("A2:T" + array.GetLength(0).ToString).Value2 = array

這提高了我的速度,但我正在處理數千行,我不確定這會增加多少代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM