使用CsvReader从LumenWorks用英寸;"定界符 [英] Using CsvReader From LumenWorks with ";" delimiter

查看:251
本文介绍了使用CsvReader从LumenWorks用英寸;"定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与 CsvReader 两个小问题,从LumenWorks。

I have two little issues with the CsvReader from LumenWorks.

首先我的一个 * CSV 文件有一个; 分隔符。没什么大不了我只需要改变在读者的分隔符属性,但它实际上是比这一点更棘手,因为头部有一个结束; 太多,但不是行。

First one my *.csv files has a ; delimiter. Not a big deal I just have to change the delimiter property in the reader but it is actually a little more tricky than that because headers ends with a ; too but not the rows.

例如:

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

有没有一种方法,以表明它的读者?

Is there a way to indicate it to the reader?

和第二个问题我怎么能选择要导入的动态列?

And second issue how can I select dynamically the columns to import?

我的code被写入如下:

My code is written as follow:

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?

But how can I make the selected column depends on values in column2Import?

感谢

推荐答案

依托 CSVReader 不是一件坏事,但如果你有特殊的要求,也许这将是更容易依靠传统的StreamReader ,而不是进行必要的修改花费时间。样品code:

Relying on a CSVReader is not a bad thing but if you have special requirements, perhaps it would be easier to rely on the conventional StreamReader rather than spending time on carrying out the required modifications. Sample code:

 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

这篇关于使用CsvReader从LumenWorks用英寸;"定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆