2017-03-03 2 views
1

私は、Excelファイルの特定の列の文字列を検索するアプリケーションを構築しようとしています。文字列が見つかった場合は、対応する列値を表示します。例:Excelファイル列の文字列を検索しています"、検索している文字列が見つかった場合は、同じ行の" E "番目の列の値を表示します。そのことは、自分のコードが2番目の形式の複数のラベルに複数の値を複数表示していることです。ラベルに複数の値を動的に表示するにはどうすればよいですか?

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

あなたの問題を正しく理解すれば、同じExcelセル値に対してイベントハンドラPanel1_Paintが複数回呼び出されます。したがって、それは単に特定の名前のラベルが既に存在するかどうかをチェックすることによって解決することができます。このように:

+0

こんにちは、私の例を含めて詳しく教えてください。私は同じテキストの複数のラベルを取得しているということです。いくつかの繰り返しの後、テキストが変更されます。 –

関連する問題