2017-03-03 60 views
1

我想建立一個應用程序,在excel文件的特定列中搜索字符串,如果找到該字符串,則顯示相應的列值對於例如:假設我正在搜索excel文件列中的字符串「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

如果我正確理解你的問題,那麼事件處理程序Panel1_Paint對於同一個excel單元格值被調用多次。所以可以簡單地通過檢查帶有particulat名稱的標籤是否已經在那裏解決,例如,像這樣:

+0

嗨,兄弟..你能否請詳細說明包括我的例子?問題是我得到了多個相同文本的標籤。重複幾次後,文字就會變化。 –

相關問題