我正在尝试构建一个搜索excel文件的特定列中的字符串的应用程序,如果找到该字符串,则显示相应的列值Ex Ex:Say我在excel文件列中搜索字符串& #34; N",如果找到我正在搜索的字符串,则显示同一行的" E"列值。事实是我的代码多次显示单个值第二种形式的多个标签。
FORM1
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop.Excel
Imports System.Globalization
Imports System.Runtime.InteropServices
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim range As Excel.Range
Dim Obj As Object
Dim pass As String
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
MessageBox.Show("You have selected" + OpenFileDialog1.FileName)
'sr.Close()
End If
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(OpenFileDialog1.FileName)
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
range = xlWorkSheet.UsedRange
For rCnt = 1 To range.Rows.Count
For cCnt = 14 To range.Columns.Count
If xlWorkSheet.Cells(rCnt, cCnt).value = "3" Or xlWorkSheet.Cells(rCnt, cCnt).value = "4" Or xlWorkSheet.Cells(rCnt, cCnt).value = "5" Or xlWorkSheet.Cells(rCnt, cCnt).value = "6" Or xlWorkSheet.Cells(rCnt, cCnt).value = "7" Or xlWorkSheet.Cells(rCnt, cCnt).value = "8" Or xlWorkSheet.Cells(rCnt, cCnt).value = "9" Or xlWorkSheet.Cells(rCnt, cCnt).value = "10" Then
Obj = CType(range.Cells(rCnt, "E"), Excel.Range)
'MessageBox.Show(Obj.value)
Foo = Obj.value
Form2.Show()
End If
Next
Next
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
FORM2
Imports System.Linq
Imports System.Drawing
Public Class Form2
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Dim label As New Label()
Dim count As Integer = Panel1.Controls.OfType(Of Label)().ToList().Count
label.Location = New Point(10, (25 * count))
'label.Size = New Size(40, 20)
label.Name = "label_" & (count + 1)
label.Text = Foo '& (count + 1)
label.AutoSize = True
Panel1.Controls.Add(label)
Dim button As New Button()
button.Location = New System.Drawing.Point(250, 25 * count)
button.Size = New System.Drawing.Size(60, 20)
button.Name = "Print" & (count + 1)
button.Text = "Print" '& (count + 1)
AddHandler button.Click, AddressOf Button_Click
Panel1.Controls.Add(button)
MessageBox.Show(Foo)
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
MessageBox.Show(button.Name + " clicked")
End Sub
End Class
模块
Module Module1
Public Foo As String
End Module
答案 0 :(得分:0)
如果我正确理解了您的问题,那么对于相同的excel单元格值,会多次调用事件处理程序Panel1_Paint
。因此,可以通过检查具有特定名称的标签是否已经存在,例如,像这样:
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
For Each c As Control In Panel1.Controls
If c Is Label Then
If ((Label) c).Text = Foo Then
' Label alredy there, exit sub...
End If
End If
Next