繁体   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