日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
VB.NET操作CSV文件實際代碼編寫

大家作為開發(fā)領(lǐng)域中的一員,應該不會不知道VB.NET這一微軟.NET系列的編程語言。它的出現(xiàn)為開發(fā)人員帶來了方便的編程環(huán)境。下面我們將會為大家詳細介紹有關(guān)VB.NET操作CSV文件的一些操作技巧。#t#

從DataTable導入到CSV

  1. Private Function ExportCsvProcess
    (ByVal FilePath As String ByVal, 
    dt As DataTable) As Boolean  
  2. Dim fileStream As System.IO.FileStream  
  3. Dim streamWriter As System.IO.StreamWriter  
  4. Dim intRow, intCol As Integer  
  5. Dim strRow As String  
  6. '刪除舊CSV文件  
  7. If (System.IO.File.Exists(FilePath)) Then  
  8. System.IO.File.Delete(FilePath)  
  9. End If  
  10. Try  
  11. fileStream = New FileStream(FilePath, 
    System.IO.FileMode.CreateNew, System.IO.
    FileAccess.Write)  
  12. If Not dt Is Nothing Then  
  13. streamWriter = New StreamWriter
    (fileStream, System.Text.Encoding.Default)  
  14. strRow = "" 
  15. '讀列名  
  16. For intCol = 0 To dt.Columns.Count - 1  
  17. strRow += dt.Columns(intCol).ColumnName  
  18. If intCol < dt.Columns.Count - 1 Then  
  19. strRow += ","  
  20. End If  
  21. Next  
  22. streamWriter.WriteLine(strRow)  
  23. '讀每行的值  
  24. For intRow = 0 To dt.Rows.Count - 1  
  25. strRow = "" 
  26. For intCol = 0 To dt.Columns.Count - 1  
  27. strRow += CStr(dt.Rows(intRow).Item(intCol))  
  28. If intCol < dt.Columns.Count - 1 Then  
  29. strRow += ","  
  30. End If  
  31. Next  
  32. streamWriter.WriteLine(strRow)  
  33. Next  
  34. streamWriter.Close()  
  35. End If  
  36. Catch ex As Exception  
  37. MessageShow(ex.ToString())  
  38. Return False  
  39. Finally  
  40. fileStream.Close()  
  41. End Try  
  42. Return True  
  43. End Function 

必要時可以進行特殊字符的過濾

VB.NET操作CSV文件中特殊字符的過濾

 
 
 
  1. Private Function DelSpacChr
    (ByVal str As String) As String  
  2. Dim i As Integer  
  3. Dim result As String = str 
  4. Dim strSpac() As String = 
    {"~", "!", "@", "#", "$", "%", 
    "^", "&", "*", "(", ")", "`", ";", 
    "'", ",", ".", "/", ":", "/,", 
    "<", ">", "?"}  
  5. For i = 0 To i < strSpac.Length 
  6. If result.IndexOf(strSpac(i)) > -1 Then  
  7. resultresult = result.Replace
    (strSpac(i), "")  
  8. End If  
  9. Next  
  10. Return result  
  11. End Function 

下面是從CSV導入到DataTable,當然還可以像上面一樣使用文件流操作,但這里采用OLEDB類實現(xiàn)VB.NET操作CSV文件。

 
 
 
  1. Public Function CSVToDataTable(ByVal 
    FilePath As String) As DataTable   
  2. Try   
  3. If (System.IO.File.Exists(FilePath)) Then   
  4. Dim fi As New System.IO.FileInfo(FilePath)   
  5. 'HDR=NO 第一行當數(shù)據(jù)處理   
  6. 'HDR=YES(默認)第一行當列處理   
  7. Dim sConnectionString As String = 
    "Provider=Microsoft.Jet.OLEDB.4.0;
    Extended Properties='Text;HDR=NO';Data 
    Source=" & fi.DirectoryName   
  8. Dim objConn As New System.Data.OleDb.
    OleDbConnection(sConnectionString) 
    objConn.Open()   
  9. Dim strColum As String   
  10. Dim objCmdSelect As New Data.OleDb.
    OleDbCommand("SELECT Distinct * FROM " 
    & fi.Name, objConn)   
  11. Dim objAdapter As New Data.OleDb.
    OleDbDataAdapter   
  12. Dim dt As New DataTable objAdapter.
    SelectCommand = objCmdSelect 
    objAdapter.Fill(dt) objConn.Close()   
  13. Return dt   
  14. End   
  15. If Catch ex As Exception   
  16. MessageShow(ex.ToString())   
  17. Return Nothing   
  18. End Try   
  19. End Function 

OK,VB.NET操作CSV文件完畢。


新聞標題:VB.NET操作CSV文件實際代碼編寫
新聞來源:http://www.dlmjj.cn/article/djsggih.html