我希望有人可以提供帮助,我一直在搜索我的问题一个多星期了。我有一个包含大量信息的excel文件,我想做的是用搜索按钮创建一个界面。因此,如果我输入日,月,年,它将为我提取该文件并在Listview中查看。我做到了这一点,但是当我更改日期时,点击我的搜索按钮,它会清除Listview,并在Listview中抛出相同的旧搜索和新搜索。
当我点击搜索时,我希望它只显示我的新搜索。
以下是我的代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ListView1 As New ListView
' ListView1 = New ListView()
ListView1.Items.Clear()
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.FullRowSelect = True
ListView1.HideSelection = False
ListView1.MultiSelect = False
ListView1.Columns.Add("Description", 160, HorizontalAlignment.Left)
ListView1.Columns.Add("QTY", 160, HorizontalAlignment.Left)
ListView1.Columns.Add("Total", 160, HorizontalAlignment.Left)
TextBox4.Clear()
TextBox4.Text = ("d:\test\" + TextBox3.Text + "_" + TextBox2.Text + ".xls")
If GetInfo() = True Then
For Each Xitem In ExcelRowList
Dim lvitem = ListView1.Items.Add(Xitem.C1)
lvitem.SubItems.AddRange(New String() {Xitem.C2, Xitem.C3})
Next
End If
'######################################## To get Total Amount in Total Box for Day Amount ######################
Dim dblTotal As Double = 0
Dim dblTemp As Double
For Each lvItem As ListViewItem In ListView1.Items
If Double.TryParse(lvItem.SubItems(2).Text, dblTemp) Then
dblTotal += dblTemp
End If
Next
TextBox5.Text = dblTotal
End Sub
Private Function GetInfo() As Boolean
Dim Completed As Boolean = False
'Open Workbook
Dim MyExcel As New Application
MyExcel.Workbooks.Open(TextBox4.Text)
'Extract Date From Workbook
MyExcel.Sheets(TextBox1.Text).Activate()
MyExcel.Range("B2").Activate()
Dim ThisRow As New ExcelRows
Do
If MyExcel.ActiveCell.Value > Nothing Or MyExcel.ActiveCell.Text > Nothing Then
ThisRow.C1 = MyExcel.ActiveCell.Value
MyExcel.ActiveCell.Offset(0, 1).Activate()
ThisRow.C2 = MyExcel.ActiveCell.Value
MyExcel.ActiveCell.Offset(0, 1).Activate()
ThisRow.C3 = MyExcel.ActiveCell.Value
ExcelRowList.Add(ThisRow)
MyExcel.ActiveCell.Offset(1, -2).Activate()
Else
Completed = True
Exit Do
End If
Loop
'Close Workbook
System.Runtime.InteropServices.Marshal.ReleaseComObject(New Application)
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
proc.Kill()
MyExcel = Nothing
ThisRow = Nothing
Next
Return Completed
End Function
答案 0 :(得分:0)
每次调用时,Dim ListView1 As New ListView
都会创建一个新的列表视图。尝试将它放在类级别,以便只调用一次。同时清除列集合以及项目集合。
同时尝试清除ExcelRowList
例程
GetInfo
在旁注中,您添加列总计的例程,您可以将其放在添加listviewitems的循环内,而不是运行另一个循环。