简体   繁体   中英

Outputing to console results from xlsx file on VB.NET

I am writing a piece of code whereby I want the following to happen:

  1. the application takes an Excel .xlsx file as input
  2. the application takes only the information you specify and outputs them row by row onto the console

Here is my code:

 Private Sub FindValueInExcel()
    Using reader As New StreamReader("D:\excelFileTest.csv")
        While Not reader.EndOfStream
            Dim line As String = reader.ReadLine()
            Dim readLine As String
            For Each readLine In line
                If readLine = "14" Then
                    Console.WriteLine(readLine)
                End If
            Next

        End While
    End Using

End Sub

The problem is the above does not display anything. The code I want it to just look at a specific column display all the rows that are 14 and any information on that row based on that column.

I think your problem is 2-part, one is that you aren't checking the csv line variable's contents correctly, and two you are looking for an entire line to be equal to "14", and if this csv file has anything more than a single column of data you'll never get a line that equals 14. So if you actually check the line variable for your value instead of readLine (which you don't need) and also use Arun Kumar's suggestion of using Contains to search the entire string line for your value, your code would look something like this:

Private Sub FindValueInExcel(ByVal FilePath As String, ByVal SearchValue As String)
    Using reader As New System.IO.StreamReader(FilePath)
        While Not reader.EndOfStream
            Dim line As String = reader.ReadLine()
            If line.Contains(SearchValue) Then
                Console.WriteLine(line)
            End If
        End While
    End Using
End Sub

Called using FindValueInExcel("D:\\excelFileTest.csv", "14")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM