2013-06-05 48 views
0

我有一个文本文件,其中有数百个潜在客户,我试图将其转换为csv进行导入。如何从格式化的文本文件中创建CSV文件

整个文档的格式。

展望名称

说明 网站

展望名称

说明 网站

我怎么会写VB程序遍历这个做一个CSV文件。每4条线是一个新的前景。

+0

要正确你的问题回答你需要显示输入文件的格式。 – Steve

+2

分享你的研究可以帮助每个人。告诉我们你发现了什么,以及它为什么不符合你的需求。这表明你已经花时间去尝试帮助自己,它使我们避免重申明显的答案,最重要的是它有助于你获得更具体和相关的答案!另请参阅[如何问](http://stackoverflow.com/questions/how-to-ask),[你有什么尝试?](http://whathaveyoutried.com)和[什么是一个很好的问题?] (http://tinyurl.com/so-hints) –

回答

0

将文件内容作为IEnumerable(String)获取,然后旋转它,为每个记录添加一个CSV行(到新的(String)列表中)。最后写出文件内容。您可能需要添加标题行。

 Dim lstLines As IEnumerable(Of String) = IO.File.ReadLines("C:\test\ConvertToCSV.txt") 

     Dim lstNewLines As New List(Of String) 

     Dim intRecordTracker As Integer = 0 

     Dim strCSVRow As String = String.Empty 

     For Each strLine As String In lstLines 

      If intRecordTracker = 4 Then 

       intRecordTracker = 0 

       'Trim off extra comma. 
       lstNewLines.Add(strCSVRow.Substring(0, (strCSVRow.Length - 1))) 

       strCSVRow = String.Empty 

      End If 

      strCSVRow += strLine & "," 

      intRecordTracker += 1 

     Next 

     'Add the last record. 
     lstNewLines.Add(strCSVRow.Substring(0, (strCSVRow.Length - 1))) 

     'Finally write the CSV file. 
     IO.File.WriteAllLines("C:\Test\ConvertedCSV.csv", lstNewLines) 
0
Dim count As Integer = -1, lstOutput As New List(Of String) 

lstOutput.AddRange(From b In File.ReadAllLines("C:\temp\intput.txt").ToList.GroupBy(Function(x) (Math.Max(Threading.Interlocked.Increment(count), count - 1) \ 4)).ToList() Select String.Join(",", b)) 

File.WriteAllLines("c:\temp\test\output.txt", lstOutput)