vb.net打开csv vs打开vb文件

请教vb.net 读csv 文件,搜索表示指定行CSV文件特征是每行一条记录,字段用逗号分开 。你只需要逐行处理,将每行文本用逗号切分得到数组array,然后判断array[0]是否等于"bbb",如果相等则array[1]就是”4561234567890“;如果不等则继续处理下一行 。
没怎么用过VB,下面给出C#代码:
string FindString(string filename, string title){FileStream stream = null;try{stream = File.OpenRead(filename);StreamReader reader = new StreamReader(stream, Encoding.Default);
string result = null;//存储查找的结果while (!reader.EndOfStream){//读取一行string strLine = reader.ReadLine();string[] array = strLine.Split(',');if (array == null || array.Length1)//该行无效continue;if (string.Compare(array[0], title, false) == 0){//找到了return array[1];}}}catch (Exception ex){//出错了return null;}finally{if (stream != null)stream.Close();}
//到这里说明没找到return null;}void Test(){string result = FindString(@"C:\demo.csv", "bbb");}
vb.net 操作CSV问题 求大神 急用一个streamreader和streamwriter即可
Using sr2 As New StreamWriter("2.csv", False, Text.Encoding.Default) '要写入的文件
Using sr1 As New StreamReader("1.csv", Text.Encoding.Default) '要读取的文件
While Not sr1.EndOfStream 'EndOfStream=True表示读取结束了
'读取
Dim lineread As String = "" '等下要读的行
Dim linewrite As String = "" '等下要写入的行
Dim data As String() '每个数据的数组
lineread = sr1.ReadLine '读一行并把流的位置往后调一行
'你现在可以用If判断这一行要不要删除 。如果要的话,用Else直接跳过下面的语句即可 。
data = https://www.04ip.com/post/lineread.Split(",".ToCharArray, StringSplitOptions.RemoveEmptyEntries) '读取一行 , 用逗号分隔后存在数组里
'下面对读取到的数据进行处理,你可以自己处理它
For Each item As String In data
linewrite = item"," 'csv是以逗号分隔的 , 我们写进去时也要记得加逗号
Next
'写入
If linewrite.EndsWith(",") Then '去掉行最后一个逗号 。
'如果之前读到一个空行,这里就不会执行 。你想想为什么
sr2.WriteLine(linewrite.Remove(linewrite.Length - 1, 1))
End If
'如果你之前用If判断了行要不要删除,那么End If就应该加在这里 。
End While
sr1.Close()
End Using
sr2.Flush()
sr2.Close()
End Using
vb.net 将csv 数据导入datagridview本例子在窗体上添加一个按钮vb.net打开csv,一个空的DataGridView控件vb.net打开csv;
在按钮的单击事件里编写代码如下:
'读取CSV文件到DataGridView控件
Dim r As New StreamReader("C:\工作簿1.csv", System.Text.Encoding.Default) '用StreamReader打开文件
Dim MyRows(0) As String
Dim d() As String
Dim i As Integer = -1
Dim j As Integer = 0
Do While r.Peek-1 '是否到文件尾
i = i1
ReDim Preserve MyRows(i)
MyRows(i) = r.ReadLine'从打开的文件中读取一行内容
Loop
r.Close()'关闭对象
'获得数据的列数
d = Split(MyRows(1), ",")
'首先在DataGridView控件中添加列
DataGridView1.ColumnCount = UBound(d)1
'然后在DataGridView控件中添加行
DataGridView1.Rows.Add(UBound(MyRows))
'在DataGridView控件中添加数据
For i = 0 To UBound(MyRows)
d = Split(MyRows(i), ",")
For j = 0 To UBound(d)
DataGridView1.Item(j, i).Value = https://www.04ip.com/post/d(j)
Next
Next
Vb.net存csv文件,文件被打开后,再用程序存储就会存存不进去参考
Private Function ExportCsvProcess
(ByVal FilePath As String ByVal,
dt As DataTable) As Boolean
Dim fileStream As System.IO.FileStream
Dim streamWriter As System.IO.StreamWriter
Dim intRow, intCol As Integer
Dim strRow As String
'删除旧CSV文件
If (System.IO.File.Exists(FilePath)) Then
System.IO.File.Delete(FilePath)
End If
Try
fileStream = New FileStream(FilePath,
System.IO.FileMode.CreateNew, System.IO.
FileAccess.Write)
If Not dt Is Nothing Then
streamWriter = New StreamWriter
(fileStream, System.Text.Encoding.Default)
strRow = ""
'读列名
For intCol = 0 To dt.Columns.Count - 1
strRow= dt.Columns(intCol).ColumnName
If intCol dt.Columns.Count - 1 Then
strRow= ","
End If
Next
streamWriter.WriteLine(strRow)
'读每行的值
For intRow = 0 To dt.Rows.Count - 1
strRow = ""
For intCol = 0 To dt.Columns.Count - 1
strRow= CStr(dt.Rows(intRow).Item(intCol))
If intCol dt.Columns.Count - 1 Then
strRow= ","
End If
Next
streamWriter.WriteLine(strRow)
Next
streamWriter.Close()
End If
Catch ex As Exception
MessageShow(ex.ToString())
Return False
Finally
fileStream.Close()
End Try
Return True
End Function
必要时可以进行特殊字符的过滤
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
【vb.net打开csv vs打开vb文件】End Try
End Function
OK,VB.NET操作CSV文件完毕 。
关于VB.NET的CSV文件的读取vb.net打开csv你是怎么读写vb.net打开csv的呢?下面是简单的读写
Dim str As String = (My.Computer.FileSystem.ReadAllText("C:\QD51-R24_A.csv")) '读
My.Computer.FileSystem.WriteAllText("C:\1.csv", str, True) '写
vb如何实时读取csv文件?给你一段VB.net读取csv文件的代码把 。
Imports System.IO
Imports System.Collections.Generic
Module Module1
Public CSV数据 As List(Of List(Of String))
''' summary
''' 从指定路径的文件读取内容 , 并分析出其中每行含有分隔符的数据,存到CSV数据中 。
''' para调用的时候使用CSV数据(5)(0)表示第6条记录的第1个数据/para
''' /summary
''' param name="文件路径"必需 。一个[String]表达式 。要读取的文件路径 。/param
''' returns返回是否读取成功/returns
''' remarks/remarks
Public Function 读取(ByVal 文件路径 As String) As Boolean
Dim 文件读取器 As StreamReader = New StreamReader(文件路径)
Dim 语句 As String
Dim t成功 As Boolean = True
Dim 分隔符 As Char = ";"
CSV数据 = New List(Of List(Of String))
While Not 文件读取器.EndOfStream
Try
语句 = 文件读取器.ReadLine
'分析语句后判断类型
If 语句.Contains(分隔符) Then
Dim array = 语句.Split(分隔符)
If array IsNot Nothing Then
CSV数据.Add(array.ToList())
End If
End If
Catch ex As Exception
t成功 = False
End Try
End While
Return t成功
End Function
End Module
你上面的数据执行后,将会保存到一个List嵌套List的String组中 。
自己转化为数字再进行操作把 。
Dim 数字 = Convert.ToDecimal(CSV数据(5)(0))
关于vb.net打开csv和vs打开vb文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读