【问题标题】:Outputing to console results from xlsx file on VB.NET从 VB.NET 上的 xlsx 文件输出到控制台结果
【发布时间】:2018-10-24 02:39:55
【问题描述】:

我正在编写一段代码,希望发生以下情况:

  1. 应用程序将 Excel .xlsx 文件作为输入
  2. 应用程序获取您指定的信息并将它们逐行输出到控制台

这是我的代码:

 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

问题是上面没有显示任何东西。我希望它只是查看特定列的代码显示所有 14 行以及基于该列的该行的任何信息。

【问题讨论】:

  • 所有 14 行 是什么意思?你想显示任何包含 14 的行吗?如果是这样,请尝试将 If readLine = "14" Then 更改为 If line.Contains("14") Then
  • 您应该将line 写入您的控制台,这是您文件中的实际文本行。目前,readLine 没有值。 line 是一个字符串,而不是一个数组/集合......所以 line 永远不会持有 14 的值
  • 更正拼写,去除噪音
  • 谢谢!我得到了这个工作,谢谢你的建议:)

标签: excel vb.net visual-studio


【解决方案1】:

我认为你的问题是两部分,一个是你没有正确检查 csv 行变量的内容,二是你正在寻找一整行等于“14”,如果这个 csv 文件有除了一列数据之外,您永远不会得到等于 14 的行。因此,如果您实际上检查 line 变量的值而不是 readLine (您不需要)并且还使用 Arun Kumar 的建议使用 Contains 在整个字符串行中搜索您的值,您的代码将如下所示:

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

使用FindValueInExcel("D:\excelFileTest.csv", "14")调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多