以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]编码合并文本文件出现乱码 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=140173) |
-- 作者:农村人 -- 发布时间:2019/8/30 19:00:00 -- [求助]编码合并文本文件出现乱码 我的文本格式是(如下): 张三
北京AAA
111
1111 李四
河北FFF
222
2222 王五
天津RH
333
3333 ...... 我通过如下代码执行合并: Dim dlg As new OpenFileDialog dlg.Filter = "文本文件|*.txt" If dlg.ShowDialog = DialogResult.OK Then Using sr As IO.StreamReader = New IO.StreamReader(dlg.FileName) \'直接从文件路径生成\' Dim line As String = sr.ReadLine() \'读取一行 Dim rs1() As String = line.Split(vbcr) For i As Integer = 0 To rs1.Length - 1 Dim cs() As String = rs1(i).Split(vbtab) If cs.Length = 4 Then Dim dr As DataRow = DataTables("表A").Addnew() dr("第一列") = cs(0) dr("第二列") = cs(1) dr("第三列") = cs(2) dr("第四列") = cs(3) End If Next Do While line IsNot Nothing \'如果不为空.为空说明读取完毕,结束循环 line = sr.ReadLine() \'读取下一行 Dim rs() As String = line.Split(vbcr) For i As Integer = 0 To rs.Length - 1 Dim cs() As String = rs(i).Split(vbtab) If cs.Length = 4 Then Dim dr As DataRow = DataTables("表A").Addnew() dr("第一列") = cs(0) dr("第二列") = cs(1) dr("第三列") = cs(2) dr("第四列") = cs(3) End If Next Loop End Using End If 能完成合并,但是出现乱码: 求助上面代码如何修改? |
-- 作者:有点蓝 -- 发布时间:2019/8/30 20:36:00 -- 编码问题可以参考: Using sr As IO.StreamReader = New IO.StreamReader(dlg.FileName,System.Text.Encoding.ASCII) Encoding类型,可选的编码有: Default 具体是那种编码,自己尝试 ------------------ Dim dlg As new OpenFileDialog dlg.Filter = "文本文件|*.txt" If dlg.ShowDialog = DialogResult.OK Then Using sr As IO.StreamReader = New IO.StreamReader(dlg.FileName,System.Text.Encoding.UTF8) \'直接从文件路径生成\' Dim line As String = sr.ReadLine() \'读取一行 Do While line IsNot Nothing \'如果不为空.为空说明读取完毕,结束循环 Dim cs() As String = line.trim().Replace(vbvr,"").Replace(vblf,"").Split(vbtab) If cs.Length = 4 Then Dim dr As DataRow = DataTables("表A").Addnew() dr("第一列") = cs(0) dr("第二列") = cs(1) dr("第三列") = cs(2) dr("第四列") = cs(3) End If line = sr.ReadLine() \'读取下一行 Loop End Using End If |