.net - 使用 LumenWorks 中的 CsvReader 和 ";"分隔符

标签 .net vb.net linq csv

我在使用 LumenWorks 的 CsvReader 时遇到两个小问题。

第一个我的 *.csv 文件有一个 ; 分隔符。没什么大不了的,我只需要更改阅读器中的分隔符属性,但实际上比这更棘手,因为标题也以 ; 结尾,但不是行。

例如:

Column1;Column2;Column3;
1;Michael;Page
2;Michael;Jackson
...

有没有办法向读者表明这一点?

第二个问题如何动态选择要导入的列?

我的代码编写如下:

Public Sub ImportCSV2Data(ByVal filename As String, ByRef gridToShow As GridControl, ByVal column2Import() As Integer)
    Dim csvCopy As CachedCsvReader = New CachedCsvReader(New StreamReader(filename), True, ";"c)
    Dim processedCopy = csvCopy.Select(Function(showColumn) New With{.SAPNo = column(0),.CCode = column(2)})

    gridToShow.DataSource = processedCopy
End Sub

但是如何使所选列取决于 column2Import 中的值?

谢谢

最佳答案

依赖CSVReader并不是一件坏事,但如果你有特殊的需求,也许依赖传统的StreamReader会更容易,而不是花时间携带进行所需的修改。示例代码:

 Dim sr As System.IO.StreamReader = New System.IO.StreamReader("target CSV file path")
 Dim line As String

 'Adapt this code to retrieve the column names from the file itself or from other source
 Dim getColumnNames As Boolean = True
 Dim columnNames() As String = Nothing

 Do
     line = sr.ReadLine()
     If (line IsNot Nothing) Then
         if(line.Contains(";")) then
             If (columnNames Is Nothing And getColumnNames) Then
                 columnNames = line.Split(";")
             Else
                 Dim curRowVals() As String = line.Split(";")
                 'All the row values
             End If
         End If
     End If
 Loop Until line Is Nothing

关于.net - 使用 LumenWorks 中的 CsvReader 和 ";"分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17897917/

相关文章:

.net - 如何将 System::array 转换为 std::vector?

.net - IEquatable(Of T)/IEqualityComparer(Of T) 未被调用

vb.net - 使用 URI 格式打开文件

c# - 带参数的表达式(LINQ、SQL、C#)

c# - linq 语法选项

c# - 列出带有实例的可用 SQL Server 不包括 SQL Server Express

.net - 正则表达式替换捕获后跟数字

c# - LINQ to Object 比较两个整数列表的不同值

vb.net - 如何将多个DataTable合并到一个DataTable中?

c# - 在 LINQ 表达式中将 Int 转换为 String