从 VB.NET 中的文本文件导入数据
Importing data from text file in VB.NET
所以,我正在尝试开发一个图书数据库。我创建了一个包含 11 列的 table 来填充 DGV,其中仅显示 6 列。每本书的完整数据显示在表格的下半部分,其中我有文本框,这些文本框与 table 绑定 (BindingSource),随着我在 DGV 中的移动而变化。
现在,我想做的是能够从文件中获取 export/import 数据。
我已经用下面的代码完成了导出部分:
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
Dim txt As String = String.Empty
For Each row As DataGridViewRow In DbdocsDataGridView.Rows
For Each cell As DataGridViewCell In row.Cells
'Add the Data rows.
txt += CStr(cell.Value & ","c)
Next
'Add new line.
txt += vbCr & vbLf
Next
Dim folderPath As String = "C:\CSV\"
File.WriteAllText(folderPath & "DataGridViewExport.txt", txt)
End Sub
但是,我无法从 txt 导入。我试过的是:https://1bestcsharp.blogspot.com/2018/04/vb.net-import-txt-file-text-to-datagridview.html
如果您对 table 进行编码并且它可以毫无问题地填充 de DGV,则它会完美运行。我看不出应该如何调整该代码以满足我的需要。
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
DbdocsDataGridView.DataSource = table
Dim filas() As String
Dim valores() As String
filas = File.ReadAllLines("C:\CSV\DataGridViewExport.txt")
For i As Integer = 0 To filas.Length - 1 Step +1
valores = filas(i).ToString().Split(",")
Dim row(valores.Length - 1) As String
For j As Integer = 0 To valores.Length - 1 Step +1
row(j) = valores(j).Trim()
Next j
table.Rows.Add(row)
Next i
End Sub
这是我迄今为止尝试过的方法,但我总是会出现异常。
提前感谢任何能给我一些见解的人。
这是写入文本文件的代码:
speichern
表示 to save
。
请注意,出于格式原因,我在示例中使用了 #
。再次阅读时,您可以找到 #
然后您就知道那一行即将到来......
我使用 Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE")
将日期格式化为德语格式,但您可以自行决定是否需要。
请注意,我使用的是从 Visual Studio 自己的 NuGet 包管理器下载的 FileDialog。
Private Sub Button_speichern_Click(sender As Object, e As EventArgs) Handles Button_speichern.Click
speichern()
End Sub
Private Sub speichern()
'-------------------------------------------------------------------------------------------------------------
' User can choose where to save the text file and the program will save it .
'-------------------------------------------------------------------------------------------------------------
Dim Path As String
Using SFD1 As New CommonSaveFileDialog
SFD1.Title = "store data in a text file"
SFD1.Filters.Add(New CommonFileDialogFilter("Textdatei", ".txt"))
SFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If SFD1.ShowDialog() = CommonFileDialogResult.Ok Then
Path = SFD1.FileName & ".txt"
Else
Return
End If
End Using
Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, True, System.Text.Encoding.UTF8)
textfile.WriteLine("Timestamp of this file [dd.mm.yyyy hh:mm:ss]: " & Date.Now.ToString("G", Deu) & NewLine & NewLine) 'supposed to be line break + 2 blank lines :-)
textfile.WriteLine("your text")
textfile.WriteLine("#") 'Marker
textfile.Close()
End Using
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
speichern()
End Sub
这是从文本文件中读取:
'read all Text
Dim RAT() As String = System.IO.File.ReadAllLines(Pfad, System.Text.Encoding.UTF8)
If RAT.Length = 0 Then Return Nothing
For i As Integer = 0 To RAT.Length - 1 Step 1
If RAT(i) = "#" OrElse RAT(i) = "" Then Continue For
'do your work here with (RAT(i))
Next
DataTable class 具有 load/save 数据 from/to XML 的内置方法,称为 ReadXml
和 WriteXml
。看一下使用重载来保留架构的示例:
Private ReadOnly dataGridViewExportPath As String = IO.Path.Combine("C:\", "CSV", "DataGridViewExport.txt")
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
table.WriteXml(path, XmlWriteMode.WriteSchema)
End Sub
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
table.ReadXml(path)
End Sub
虽然用户可以手动编辑由 WriteXML 生成的 XML 文件,但我当然不建议这样做。
所以,我正在尝试开发一个图书数据库。我创建了一个包含 11 列的 table 来填充 DGV,其中仅显示 6 列。每本书的完整数据显示在表格的下半部分,其中我有文本框,这些文本框与 table 绑定 (BindingSource),随着我在 DGV 中的移动而变化。
现在,我想做的是能够从文件中获取 export/import 数据。
我已经用下面的代码完成了导出部分:
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
Dim txt As String = String.Empty
For Each row As DataGridViewRow In DbdocsDataGridView.Rows
For Each cell As DataGridViewCell In row.Cells
'Add the Data rows.
txt += CStr(cell.Value & ","c)
Next
'Add new line.
txt += vbCr & vbLf
Next
Dim folderPath As String = "C:\CSV\"
File.WriteAllText(folderPath & "DataGridViewExport.txt", txt)
End Sub
但是,我无法从 txt 导入。我试过的是:https://1bestcsharp.blogspot.com/2018/04/vb.net-import-txt-file-text-to-datagridview.html 如果您对 table 进行编码并且它可以毫无问题地填充 de DGV,则它会完美运行。我看不出应该如何调整该代码以满足我的需要。
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
DbdocsDataGridView.DataSource = table
Dim filas() As String
Dim valores() As String
filas = File.ReadAllLines("C:\CSV\DataGridViewExport.txt")
For i As Integer = 0 To filas.Length - 1 Step +1
valores = filas(i).ToString().Split(",")
Dim row(valores.Length - 1) As String
For j As Integer = 0 To valores.Length - 1 Step +1
row(j) = valores(j).Trim()
Next j
table.Rows.Add(row)
Next i
End Sub
这是我迄今为止尝试过的方法,但我总是会出现异常。
提前感谢任何能给我一些见解的人。
这是写入文本文件的代码:
speichern
表示 to save
。
请注意,出于格式原因,我在示例中使用了 #
。再次阅读时,您可以找到 #
然后您就知道那一行即将到来......
我使用 Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE")
将日期格式化为德语格式,但您可以自行决定是否需要。
请注意,我使用的是从 Visual Studio 自己的 NuGet 包管理器下载的 FileDialog。
Private Sub Button_speichern_Click(sender As Object, e As EventArgs) Handles Button_speichern.Click
speichern()
End Sub
Private Sub speichern()
'-------------------------------------------------------------------------------------------------------------
' User can choose where to save the text file and the program will save it .
'-------------------------------------------------------------------------------------------------------------
Dim Path As String
Using SFD1 As New CommonSaveFileDialog
SFD1.Title = "store data in a text file"
SFD1.Filters.Add(New CommonFileDialogFilter("Textdatei", ".txt"))
SFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If SFD1.ShowDialog() = CommonFileDialogResult.Ok Then
Path = SFD1.FileName & ".txt"
Else
Return
End If
End Using
Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, True, System.Text.Encoding.UTF8)
textfile.WriteLine("Timestamp of this file [dd.mm.yyyy hh:mm:ss]: " & Date.Now.ToString("G", Deu) & NewLine & NewLine) 'supposed to be line break + 2 blank lines :-)
textfile.WriteLine("your text")
textfile.WriteLine("#") 'Marker
textfile.Close()
End Using
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
speichern()
End Sub
这是从文本文件中读取:
'read all Text
Dim RAT() As String = System.IO.File.ReadAllLines(Pfad, System.Text.Encoding.UTF8)
If RAT.Length = 0 Then Return Nothing
For i As Integer = 0 To RAT.Length - 1 Step 1
If RAT(i) = "#" OrElse RAT(i) = "" Then Continue For
'do your work here with (RAT(i))
Next
DataTable class 具有 load/save 数据 from/to XML 的内置方法,称为 ReadXml
和 WriteXml
。看一下使用重载来保留架构的示例:
Private ReadOnly dataGridViewExportPath As String = IO.Path.Combine("C:\", "CSV", "DataGridViewExport.txt")
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
table.WriteXml(path, XmlWriteMode.WriteSchema)
End Sub
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
table.ReadXml(path)
End Sub
虽然用户可以手动编辑由 WriteXML 生成的 XML 文件,但我当然不建议这样做。