VB.NET操作CSV文件实际代码编写

开发 后端
我们会为大家以代码的形式详细讲解有关VB.NET操作CSV文件的一些实现技巧,方便大家理解,听从中学习到一些关于VB.NET的应用技巧,提高大家的学习速度。

大家作为开发领域中的一员,应该不会不知道VB.NET这一微软.NET系列的编程语言。它的出现为开发人员带来了方便的编程环境。下面我们将会为大家详细介绍有关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类实现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 第一行當數據處理   
  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文件完畢。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-15 11:31:02

VB.NET接口实现多

2010-01-14 14:46:57

2010-01-11 10:44:47

VB.NET多窗体

2010-01-11 17:58:36

VB.NET压缩ZIP

2010-01-18 14:35:11

VB.NET读取内存

2010-01-14 09:55:06

VB.NET IEnu

2010-01-15 19:04:09

2009-10-28 13:24:25

VB.NET文件

2009-10-29 15:28:38

VB.NET文件操作

2010-01-11 10:19:18

VB.NET启动外部程

2010-01-20 13:42:10

VB.NET访问INIGetPrivateP

2010-01-15 16:21:45

VB.NET读写文本文

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网页计数器

2010-01-11 14:16:14

VB.NET生成验证码

2010-01-07 18:05:18

VB.NET事务处理

2010-01-08 15:22:22

VB.NET局部变量

2009-10-14 10:08:05

VB.NET编写DEC
点赞
收藏

51CTO技术栈公众号