2013-06-05 51 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)