大家作为开发领域中的一员,应该不会不知道VB.NET这一微软.NET系列的编程语言。它的出现为开发人员带来了方便的编程环境。下面我们将会为大家详细介绍有关VB.NET操作CSV文件的一些操作技巧。#t#
从DataTable导入到CSV
必要时可以进行特殊字符的过滤
VB.NET操作CSV文件中特殊字符的过滤
- Private Function DelSpacChr
(ByVal str As String) As String- Dim i As Integer
- Dim result As String = str
- Dim strSpac() As String =
{"~", "!", "@", "#", "$", "%",
"^", "&", "*", "(", ")", "`", ";",
"'", ",", ".", "/", ":", "/,",
"<", ">", "?"}- For i = 0 To i < strSpac.Length
- If result.IndexOf(strSpac(i)) > -1 Then
- resultresult = result.Replace
(strSpac(i), "")- End If
- Next
- Return result
- End Function
下面是从CSV导入到DataTable,当然还可以像上面一样使用文件流操作,但这里采用OLEDB类实现VB.NET操作CSV文件。
- Public Function CSVToDataTable(ByVal
FilePath As String) As DataTable- Try
- If (System.IO.File.Exists(FilePath)) Then
- Dim fi As New System.IO.FileInfo(FilePath)
- 'HDR=NO 第一行當數據處理
- 'HDR=YES(默認)第一行當列處理
- Dim sConnectionString As String =
"Provider=Microsoft.Jet.OLEDB.4.0;
Extended Properties='Text;HDR=NO';Data
Source=" & fi.DirectoryName- Dim objConn As New System.Data.OleDb.
OleDbConnection(sConnectionString)
objConn.Open()- Dim strColum As String
- Dim objCmdSelect As New Data.OleDb.
OleDbCommand("SELECT Distinct * FROM "
& fi.Name, objConn)- Dim objAdapter As New Data.OleDb.
OleDbDataAdapter- Dim dt As New DataTable objAdapter.
SelectCommand = objCmdSelect
objAdapter.Fill(dt) objConn.Close()- Return dt
- End
- If Catch ex As Exception
- MessageShow(ex.ToString())
- Return Nothing
- End Try
- End Function
OK,VB.NET操作CSV文件完畢。
网页标题:VB.NET操作CSV文件实际代码编写
本文链接:http://www.shufengxianlan.com/qtweb/news11/455611.html
成都网站建设公司_创新互联,为您提供全网营销推广、网站设计公司、网页设计公司、ChatGPT、响应式网站、网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联