-- 作者:cnsjroom
-- 发布时间:2022/3/1 14:18:00
-- 对于文本数据中 在导入文本数据时 没有明显|类似间隔符号 只有空格符号 该怎么写这个符号呢?
对于文本数据中 没有明显|类似间隔符号 只有空格符号 该怎么写这个符号呢?
此主题相关图片如下:11.png
当前代码如下:
Dim dlg As new OpenFileDialog dlg.Filter = "文本文件|*.txt" Dim t2 As WinForm.TextBox = e.Form.Controls("TextBox2") Dim cm4 As WinForm.ComboBox = e.Form.Controls("ComboBox4") Dim Multi As String If dlg.ShowDialog = DialogResult.OK Then
Dim strs As String = FileSys.ReadAllText(dlg.FileName, Encoding.Default) t2.text=dlg.FileName Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) For i1 As Integer = 0 To 0 Dim rs As String = Values11(0) For i As Integer = 0 To 0 Dim cs() As String = rs.Split(vbtab) output.Show(cs(0)) Multi = cs(0) Next Next cm4.ComboList=Multi End If Dim Values() As String Values = Multi.split(" ")
For Index1 As Integer = 0 To Values.Length - 1 MessageBox.Show(Values(Index1)) Next
输出得到的只有一个值
账号
户名 …… 后面的数据没有显示出来
麻烦老师帮帮看看 谢谢!
|
-- 作者:cnsjroom
-- 发布时间:2022/3/1 15:49:00
-- 回复:(有点蓝)If dlg.ShowDialog = Di...
按照老师的指导 解决了cm4 组合框的列值显示
因为文本不是固定的间隔符,新增了t3这个控件来自定义输入间隔符,下述红色代码怎么能够实现 输入的间隔符号不同 都能够正确的导入文本数据呢?
当前代码运行效果:
此主题相关图片如下:22.png
当前代码:
Dim t2 As WinForm.TextBox = e.Form.Controls("TextBox2") Dim t3 As WinForm.TextBox = e.Form.Controls("TextBox3") Dim t1 As WinForm.TextBox = e.Form.Controls("TextBox1") Dim cm4 As WinForm.ComboBox = e.Form.Controls("ComboBox4") Dim ck As WinForm.CheckBox = e.Form.Controls("是否批量导入") Dim tb1 As WinForm.Table = e.Form.Controls("Table1") Dim bsbs As String If t1.text<>"" Then If ck.Checked=True Then \'选择批量导入 If t3.text="" Then bsbs ="|" Else If t3.text<>"" Then bsbs=t3.text End If \'先从其中一个文本里面读取字段列,然后判断表是否存在数据库中,如果不存在,就创建,反之就不创建表 Dim Multi As String Dim strs As String = FileSys.ReadAllText(t2.text, Encoding.Default) t2.text=t2.text Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) For i1 As Integer = 0 To 0 Dim rs As String = Values11(0) For i As Integer = 0 To 0 Dim cs() As String = rs.Split(vbtab) output.Show(cs(0)) Multi = cs(0) Next Next Dim ab As String="否" Dim lst1 As List(Of String) lst1 = Connections("委").GetTableNames For Each nm1 As String In lst1 output.show(nm1) If t1.text=nm1 Then ab="是" End If Next Dim Values() As String ’Values = Multi.split("|")
Values = Multi.split(bsbs) If ab="是" Then Else Dim Builder As New ADOXBuilder("委") \'要指定数据源名称 Dim tbl As ADOXTable Builder.Open() tbl = Builder.NewTable(t1.text) \'创建表 With tbl For Index1 As Integer = 0 To Values.Length - 1 .AddColumn(Values(Index1) ,ADOXType.String, 255) Next End With Builder.AddTable(tbl, True, True) \'增加表 Builder.Close() Dim Builder1 As New ADOXBuilder("委") \'要指定数据源名称 Builder1.Open() \'打开ADOXBuilder With Builder1.Tables(t1.text) .AddColumn("日期" ,ADOXType.DateTime) \'增加日期型列 End With Builder1.Close() \'关闭ADOXBuilder End If \'遍历选择的文件所在根目录,然后遍历目录下的所有文件,然后开始赋值 For Each File As String In FileSys.GetFiles(FileSys.GetParentPath(t2.text)) MessageBox.Show(file) Dim strs1 As String = FileSys.ReadAllText(File, Encoding.Default) \'t2.text=FileSys.GetParentPath(t2.text) Dim Values111() As String = strs1.Replace(vbcr,"").split(vblf) Dim cmd As New S QLC ommand cmd.Conn ecti onN ame = "委" Dim dt As DataTable Dim bbb As String ="S ELECT * Fr om {" & t1.text & "}"" " cmd.CommandText = bbb dt = cmd.ExecuteReader(True) \'Dim tb1 As WinForm.Table = e.Form.Controls("Table1") tb1.Table.DataSource = dt tb1.Table.StopRedraw For i As Integer = 1 To Values111.length - 1 Dim cs() As String = Values111(i).split(bsbs) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next Next tb1.Table.ResumeRedraw tb1.Table.DataTable.Save Next Else Dim Multi As String Dim strs As String = FileSys.ReadAllText(t2.text, Encoding.Default) t2.text=t2.text Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) For i1 As Integer = 0 To 0 Dim rs As String = Values11(0) For i As Integer = 0 To 0 Dim cs() As String = rs.Split(vbtab) output.Show(cs(0)) Multi = cs(0) Next Next If t3.text="" Then bsbs ="|" Else If t3.text<>"" Then bsbs=t3.text End If Dim ab As String="否" Dim lst As List(Of String) lst = Connections("委").GetTableNames For Each nm As String In lst output.show(nm) If t1.text=nm Then ab="是" End If Next Dim Values() As String Values = Multi.split(bsbs) \'Values = Multi.split("|") If ab="是" Then Else Dim Builder As New ADOXBuilder("委") \'要指定数据源名称 Dim tbl As ADOXTable Builder.Open() tbl = Builder.NewTable(t1.text) \'创建表 With tbl For Index1 As Integer = 0 To Values.Length - 1 .AddColumn(Values(Index1) ,ADOXType.String, 255) Next End With Builder.AddTable(tbl, True, True) \'增加表 Builder.Close() Dim Builder1 As New ADOXBuilder("委") \'要指定数据源名称 Builder1.Open() \'打开ADOXBuilder With Builder1.Tables(t1.text) .AddColumn("日期" ,ADOXType.DateTime) \'增加日期型列 End With Builder1.Close() \'关闭ADOXBuilder End If Dim cmd As New SQ L Command cmd.Connec tionN ame = "委" Dim dt As DataTable Dim bbb As String ="SE LECT * Fro m {" & t1.text & "}"" " cmd.CommandText = bbb dt = cmd.ExecuteReader(True) \'Dim tb1 As WinForm.Table = e.Form.Controls("Table1") tb1.Table.DataSource = dt tb1.Table.StopRedraw For i As Integer = 1 To Values11.length - 1 Dim cs() As String = Values11(i).split(bsbs) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next Next tb1.Table.ResumeRedraw End If tb1.Table.DataTable.Save MessageBox.Show("操作完毕","温馨提示") Else MessageBox.Show("请先输入拟导入表名!","温馨提示") End If
|
-- 作者:cnsjroom
-- 发布时间:2022/3/1 17:26:00
-- 回复:(有点蓝)vbtab没有办法作为一个字符放到文本框...
按老师提示 问题还是依旧……麻烦老师指导下 另外代码有些庸大了 有没有办法精简呢
Dim t2 As WinForm.TextBox = e.Form.Controls("TextBox2") Dim t3 As WinForm.TextBox = e.Form.Controls("TextBox3") Dim t1 As WinForm.TextBox = e.Form.Controls("TextBox1") Dim cm4 As WinForm.ComboBox = e.Form.Controls("ComboBox4") Dim ck As WinForm.CheckBox = e.Form.Controls("是否批量导入") Dim tb1 As WinForm.Table = e.Form.Controls("Table1") Dim jg As WinForm.CheckBox = e.Form.Controls("是否自定义间隔符") Dim bsbs As String If t1.text<>"" Then If ck.Checked=True Then \'选择批量导入 \'先从其中一个文本里面读取字段列,然后判断表是否存在数据库中,如果不存在,就创建,反之就不创建表 Dim Multi As String Dim strs As String = FileSys.ReadAllText(t2.text, Encoding.Default) t2.text=t2.text Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) For i1 As Integer = 0 To 0 Dim rs As String = Values11(0) For i As Integer = 0 To 0 Dim cs() As String = rs.Split(vbtab) output.Show(cs(0)) Multi = cs(0) Next Next Dim ab As String="否" Dim lst1 As List(Of String) lst1 = Connections("委").GetTableNames For Each nm1 As String In lst1 output.show(nm1) If t1.text=nm1 Then ab="是" End If Next Dim Values() As String If jg.Checked=True Then Values = Multi.split(t3.text) Else Values = Multi.split("|") End If If ab="是" Then Else Dim Builder As New ADOXBuilder("委") \'要指定数据源名称 Dim tbl As ADOXTable Builder.Open() tbl = Builder.NewTable(t1.text) \'创建表 With tbl For Index1 As Integer = 0 To Values.Length - 1 .AddColumn(Values(Index1) ,ADOXType.String, 255) Next End With Builder.AddTable(tbl, True, True) \'增加表 Builder.Close() Dim Builder1 As New ADOXBuilder("委") \'要指定数据源名称 Builder1.Open() \'打开ADOXBuilder With Builder1.Tables(t1.text) .AddColumn("日期" ,ADOXType.DateTime) \'增加日期型列 End With Builder1.Close() \'关闭ADOXBuilder End If \'遍历选择的文件所在根目录,然后遍历目录下的所有文件,然后开始赋值 For Each File As String In FileSys.GetFiles(FileSys.GetParentPath(t2.text)) MessageBox.Show(file) Dim strs1 As String = FileSys.ReadAllText(File, Encoding.Default) \'t2.text=FileSys.GetParentPath(t2.text) Dim Values111() As String = strs1.Replace(vbcr,"").split(vblf) Dim cmd As New SQL Command cmd.Conn ect i Dim dt As DataTable Dim bbb As String ="SELE CT * From {" & t1.text & "}"" " cmd.CommandText = bbb dt = cmd.ExecuteReader(True) \'Dim tb1 As WinForm.Table = e.Form.Controls("Table1") tb1.Table.DataSource = dt tb1.Table.StopRedraw For i As Integer = 1 To Values111.length - 1 If jg.Checked=True Then Dim cs() As String = Values111(i).split(vbtab) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next Else Dim cs() As String = Values111(i).split(t3.text) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next End If \'Dim cs() As String = Values111(i).split("|") Next tb1.Table.ResumeRedraw tb1.Table.DataTable.Save Next Else Dim Multi As String Dim strs As String = FileSys.ReadAllText(t2.text, Encoding.Default) t2.text=t2.text Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) For i1 As Integer = 0 To 0 Dim rs As String = Values11(0) For i As Integer = 0 To 0 Dim cs() As String = rs.Split(vbtab) output.Show(cs(0)) Multi = cs(0) Next Next Dim ab As String="否" Dim lst As List(Of String) lst = Connections("委").GetTableNames For Each nm As String In lst output.show(nm) If t1.text=nm Then ab="是" End If Next Dim Values() As String If jg.Checked=True Then Values = Multi.split(t3.text) Else Values = Multi.split("|") End If If ab="是" Then Else Dim Builder As New ADOXBuilder("委") \'要指定数据源名称 Dim tbl As ADOXTable Builder.Open() tbl = Builder.NewTable(t1.text) \'创建表 With tbl For Index1 As Integer = 0 To Values.Length - 1 .AddColumn(Values(Index1) ,ADOXType.String, 255) Next End With Builder.AddTable(tbl, True, True) \'增加表 Builder.Close() Dim Builder1 As New ADOXBuilder("委") \'要指定数据源名称 Builder1.Open() \'打开ADOXBuilder With Builder1.Tables(t1.text) .AddColumn("日期" ,ADOXType.DateTime) \'增加日期型列 End With Builder1.Close() \'关闭ADOXBuilder End If Dim cmd As New SQL Command cmd.Co nnec ti Dim dt As DataTable Dim bbb As String ="SE LECT * From {" & t1.text & "}"" " cmd.Comman dText = bbb dt = cmd.ExecuteReader(True) \'Dim tb1 As WinForm.Table = e.Form.Controls("Table1") tb1.Table.DataSource = dt tb1.Table.StopRedraw For i As Integer = 1 To Values11.length - 1 \'Dim cs() As String = Values11(i).split("|") If jg.Checked=True Then Dim cs() As String = Values11(i).split(vbtab) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next Else Dim cs() As String = Values11(i).split(t3.text) Dim r As Row = tb1.Table.AddNew For i1 As Integer = 0 To math.min(cs.Length-1, tb1.Table.cols.count - 1) Dim bb As String=cs(i1).trim() If bb > "" Then If bb.Length=8 Then If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb.insert(6,"-").insert(4,"-")) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If Else If Values(i1).Contains(cm4.text) Then \'If Values(i1).Contains(cm4.text) OrElse Values(i1).Contains("日期") Then Dim d As Date = cdate(bb) r("日期")= Format(d, "yyyy-MM-dd") r(i1) = cs(i1) Else r(i1) = cs(i1) End If End If End If Next End If Next tb1.Table.ResumeRedraw End If tb1.Table.DataTable.Save MessageBox.Show("操作完毕","温馨提示") Else MessageBox.Show("请先输入拟导入表名!","温馨提示") End If
|
-- 作者:cnsjroom
-- 发布时间:2022/3/1 18:08:00
-- 回复:(有点蓝)Dim jg As WinForm.CheckBox = e.For...
老师 参照你的提示 修改后 运行效果如下: 附上例子 麻烦老师指导下
以下内容是专门发给有点蓝浏览
[UserName=有点蓝]
当值勾选自定义间隔符后,导入数据 运行出错!
此主题相关图片如下:11.png
默认导入 运行效果如图:
此主题相关图片如下:22.png
选择文件按钮代码:【运行也得不到对应的cm4正确值了】
Dim dlg As new OpenFileDialog dlg.Filter = "文本文件|*.txt" Dim t2 As WinForm.TextBox = e.Form.Controls("TextBox2") Dim t3 As WinForm.TextBox = e.Form.Controls("TextBox3") Dim cm4 As WinForm.ComboBox = e.Form.Controls("ComboBox4") Dim jg As WinForm.CheckBox = e.Form.Controls("是否自定义间隔符") Dim bsbs As String
Dim Multi As String If dlg.ShowDialog = DialogResult.OK Then t2.text=dlg.FileName Dim strs As String = FileSys.ReadAllText(t2.text, Encoding.Default) Dim Values11() As String = strs.Replace(vbcr,"").split(vblf) Dim rs As String = Values11(0) If jg.checked Then bsbs = vbtab Dim cs1() As String = rs.Split(bsbs) cm4.ComboList=String.join("|",cs1) Else If e.Form.Controls("TextBox3").text > "" \'如果指定了分隔符 bsbs = e.Form.Controls("TextBox3").text Dim cs2() As String = rs.Split(bsbs) cm4.ComboList=String.join("|",cs2) Else bsbs = "|" \'如果既不勾选,也不指定了分隔符,默认使用"|" Dim cs3() As String = rs.Split("|") output.Show(cs3(0)) Multi = cs3(0) cm4.ComboList=Multi End If End If
运行效果如下: 此主题相关图片如下:1.png
此主题相关图片如下:2.png
此主题相关图片如下:3.png
[此贴子已经被作者于2022/3/1 18:51:10编辑过]
|