我在使用 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/