【发布时间】:2020-06-24 02:00:01
【问题描述】:
是否可以在不使用循环的情况下读取 excel 项目中的所有项目?我的 Excel 中有将近 2 万行,将项目放入 Listview 需要很长时间。
这是我当前的代码:
xlWorkBook = xlApp.Workbooks.Open(FileName)
xlWorkSheet = xlWorkBook.Worksheets("ExportedFromDatGrid")
xlApp.Sheets("ExportedFromDatGrid").activate()
xlApp.Range("A2").Activate()
Dim cCount As Integer
Dim azr As Microsoft.Office.Interop.Excel.Range
azr = xlWorkSheet.UsedRange
For cCount = 2 To azr.Rows.Count
Dim newitem As New ListViewItem()
Dim excelvalue As String = (Format(azr.Cells(cCount, 2).value, "yyyy-MM-dd"))
Dim fromdate As String
fromdate = dtpFrom.Value.ToString("yyyy-MM-dd")
Dim todate As String
todate = dtpTo.Value.ToString("yyyy-MM-dd")
'MessageBox.Show(fromdate & "FROM - << TO- >>>>" & todate)
If ((excelvalue >= fromdate) And (excelvalue <= todate)) Then
newitem.Text = "CGC-" & azr.Cells(cCount, 1).value.ToString
newitem.SubItems.Add(Format(azr.Cells(cCount, 2).value, "yyyy-MM-dd HH:mm:00"))
newitem.SubItems.Add(azr.Cells(cCount, 3).value.ToString)
newitem.SubItems.Add(azr.Cells(cCount, 4).value.ToString)
newitem.SubItems.Add(azr.Cells(cCount, 5).value.ToString)
lvAll.Items.Add(newitem)
End If
Next
xlWorkBook.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(azr)
【问题讨论】:
-
我敢打赌它确实需要太长时间;但是哪个用户会滚动浏览 20,000 条记录?
-
这段代码真的有效吗,唯一的问题是它很慢?
-
@Mary 是的,代码可以运行,但速度很慢。在listview上放20k条记录大约需要10分钟,20k条记录是今年员工的出勤情况。
标签: vb.net office-interop