自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

VB.NET操作CSV文件實(shí)際代碼編寫(xiě)

開(kāi)發(fā) 后端
我們會(huì)為大家以代碼的形式詳細(xì)講解有關(guān)VB.NET操作CSV文件的一些實(shí)現(xiàn)技巧,方便大家理解,聽(tīng)從中學(xué)習(xí)到一些關(guān)于VB.NET的應(yīng)用技巧,提高大家的學(xué)習(xí)速度。

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

從DataTable導(dǎo)入到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 

 

必要時(shí)可以進(jìn)行特殊字符的過(guò)濾

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

  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導(dǎo)入到DataTable,當(dāng)然還可以像上面一樣使用文件流操作,但這里采用OLEDB類實(shí)現(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 第一行當(dāng)數(shù)據(jù)處理   
  6. 'HDR=YES(默認(rèn))第一行當(dāng)列處理   
  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文件完畢。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-01-15 11:31:02

VB.NET接口實(shí)現(xiàn)多

2010-01-14 14:46:57

2010-01-11 10:44:47

VB.NET多窗體

2010-01-18 14:35:11

VB.NET讀取內(nèi)存

2010-01-11 17:58:36

VB.NET壓縮ZIP

2009-10-28 13:24:25

VB.NET文件

2010-01-15 19:04:09

2009-10-29 15:28:38

VB.NET文件操作

2010-01-11 10:19:18

VB.NET啟動(dòng)外部程

2010-01-14 09:55:06

VB.NET IEnu

2010-01-20 13:42:10

VB.NET訪問(wèn)INIGetPrivateP

2010-01-15 16:21:45

VB.NET讀寫(xiě)文本文

2010-01-12 09:51:07

VB.NET操作dbf

2010-01-07 10:46:27

VB.NET Sock

2010-01-14 13:51:03

2010-01-18 11:03:17

VB.NET網(wǎng)頁(yè)計(jì)數(shù)器

2010-01-11 14:16:14

VB.NET生成驗(yàn)證碼

2009-10-14 10:08:05

VB.NET編寫(xiě)DEC

2009-10-12 10:04:53

VB.NET List

2010-01-07 16:16:03

VB.NET變量作用域
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)