如何将数据从VB.net中的DataGridView导出到CSV文件
问题描述:
我是VB.net编程的新手,想知道是否有人建议我如何通过命令按钮将数据从DataGridView导出到CSV文件。
任何建议或示例代码都会非常感激。
非常感谢。
保罗。
Hi,
I am new to VB.net programming and was wondering if anybody has any suggestions as to how I can export data from a DataGridView via a command button to a CSV file.
Any suggestions or sample code would be really appreciated.
Many thank.
Paul.
答
你有没有解决问题的方法???
实际上我也需要同样的解决方案。
如果你得到那个plz forword那么。
Did u get the solution of your problem???
Actually I am also required the same kind of solution.
If you u get that plz forword that to me.
我在VB.NET中使用CSV文件和DataGridView。由于有很多复杂性,我成功地将5列CSV文件检索到DataGridView中。需要说明的是,dgv中的最后两列是Combo类型。
我的目标是在检索csv文件后,我将编辑其他两个选择列并将修改后的文件导出到不同的位置。
CSV文件的内容如下:
2014001,Mensch,Samantha,CD模型基督教价值观,S,*
2014001," Mensch,Samantha,CD在祈祷时显示敬畏,S,
2014001,Mensch,Samantha,CD接受责任,S,
2014001,Mensch ,Samantha",CD Practices self control,S,
2014001,Mensch,Samantha,CD观察学校规则,S,*
2014001,Mensch, Samantha",CD尊重财产,S,
2014001,Mensch,Samantha,CD尊重他人,S,
2014001,Mensch,Samantha,用途明智的时间,S,
2014001,Mensch,Samantha,显示努力,S,
2014001,Mensch,Samantha,在团队中运作良好, S,
2014001," Mensch,Samantha,独立工作,S,
阅读CSV文件的代码如下按钮:
Sub Action_Button
Dim lineNumber As Integer = 0
Dim line As String
Dim lToken()As String
Dim sTemp As字符串
Dim sGridRow(4)字符串
OpenFileDialog1.InitialDirectory =" c:\ temp \"
''openFileDialog1.Filter ="所有文件(*。*)| *。*"
OpenFileDialog1.Filter =" CSV文件(* .csv)| *。 CSV"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If(OpenFileDialog1.ShowDialog()= Windows.Forms.DialogResult。 OK)然后
fName = OpenFileDialog1.FileName
Me.Text = fName
结束如果
Me.Text = fName
尝试
Dim reader As IO.StreamReader = New IO.StreamReader(fName)
''DataGridView2.Rows.Remove()
DataGridView2。 Rows.Clear()
虽然reader.Peek<> -1
line = reader.ReadLine()
lToken =拆分(行,,)
我作为整数= 0到lToken.Length - 3
sTemp =替换(lToken(i),"""","")
sGridRow(i)= sTemp
下一页
Me.DataGridView2.Rows.Add(sGridRow)
lineNumber + = 1
结束时
''Me.Text = Str(lineNumber)
reader.Close()
Catch ex作为例外
MessageBox.Show(读取csv文件时出错。+ ex.ToString)
结束尝试
结束Sub
上面的代码工作正常,请参阅附件。
现在我在修改dgv中的行后面临问题;假设我从组合中选择了一个成绩。现在我想将文件导出到选定位置,与我导入的名称相同。
我可以从dvg导入数据。我在这里给出了代码来填充dvg行。我需要整个dvg数据来读取和写入文件。
我不知道的代码...如何将dbg中的数据从第一行传递到最后一行?我写的代码粘贴在这里,但它只是填充当前的行列。
Private Sub SaveGridDataInFile(ByRef fName As String)
''Dim writer As IO.StreamWriter = New IO.StreamWriter(fName)
Dim I As Integer = 0
Dim j As Integer = 0
Dim cellvalue
Hi, I was working with CSV file and DataGridView in VB.NET. Having lots fo complexities i succeeded to retrieve a 5 columns CSV file into DataGridView. It is to be mentioned that the last two columns in the dgv are Combo type.
My goal was after retrieving the csv file I will edit other two selection columns and export the modified file in a different locations.
the content of the CSV file is as follows:
2014001,"Mensch, Samantha",CD Models Christian values,S,*
2014001,"Mensch, Samantha",CD Shows reverence during pray,S,
2014001,"Mensch, Samantha",CD Accepts responsibility for,S,
2014001,"Mensch, Samantha",CD Practices self control,S,
2014001,"Mensch, Samantha",CD Observes school rules,S,*
2014001,"Mensch, Samantha",CD Respects property,S,
2014001,"Mensch, Samantha",CD Respects others,S,
2014001,"Mensch, Samantha",Uses time wisely,S,
2014001,"Mensch, Samantha",Displays effort,S,
2014001,"Mensch, Samantha",Works well in a group,S,
2014001,"Mensch, Samantha",Works well independently,S,
Code for the Reading CSV file as follows under a button:
Sub Action_Button
Dim lineNumber As Integer = 0
Dim line As String
Dim lToken() As String
Dim sTemp As String
Dim sGridRow(4) As String
OpenFileDialog1.InitialDirectory = "c:\temp\"
''openFileDialog1.Filter = "All files (*.*)|*.*"
OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
Me.Text = fName
End If
Me.Text = fName
Try
Dim reader As IO.StreamReader = New IO.StreamReader(fName)
''DataGridView2.Rows.Remove()
DataGridView2.Rows.Clear()
While reader.Peek <> -1
line = reader.ReadLine()
lToken = Split(line, ",")
For i As Integer = 0 To lToken.Length - 3
sTemp = Replace(lToken(i), """", "")
sGridRow(i) = sTemp
Next
Me.DataGridView2.Rows.Add(sGridRow)
lineNumber += 1
End While
''Me.Text = Str(lineNumber)
reader.Close()
Catch ex As Exception
MessageBox.Show("Error occured while reading the csv file." + ex.ToString)
End Try
End Sub
The above code works fine, Please see the attached.
Now I face the problem after modifying the row in the dgv; suppose I have selected a grade from the combo. Now I want to export the file in a selected location as with the same name as I imported.
I can import the file taking the data from the dvg. I gave the code here to populate the dvg rows. I need entire dvg data to read and write them in the file.
The code I don know...How to take the data from the dbg from the first row to the last? The code I wrote is pasted here, but it simply populates the current row column.
Private Sub SaveGridDataInFile(ByRef fName As String)
''Dim writer As IO.StreamWriter = New IO.StreamWriter(fName)
Dim I As Integer = 0
Dim j As Integer = 0
Dim cellvalue
Dim rowLine As String =""
尝试
FileOpen(1,fName,OpenMode.Output)''打开文件输出。
''WriteLine(1," Hello",""," World")''用空格分隔字符串。
''WriteLine (1,SPC(5),5个前导空格)''打印五个前导空格。
''WriteLine(1,TAB(10),Hello)''打印第10栏中的单词。
''FileClose(1)''关闭文件。
''sData = DataGridView1.Columns(1).Name.ToString ()
'sData = Me.DataGridView1.CurrentRow.Cells(i).Value.ToStrin g
''MessageBox.Show(sData)
for j = 0 To(DataGridView2 .Rows.Count - 2)
For I = 0 To(DataGridView2.Columns.Count - 1)
If Not TypeOf DataGridView2.CurrentRow.Cells.Item(I) .Value是DBNull然后
cellvalue = DataGridView2.CurrentRow.Cells.Item(I).Value.ToStr ing
Else
cellvalue =" ;"
结束如果
''DataGridView2.CurrentRow.Cells.Item(I).Value = cellvalue
rowLine = rowLine + cellvalue +","
Next
WriteLine(1,TAB(0),rowLine)''在第10列打印单词。
''manage.WriteLine()
rowLine =""
下一页
FileClose(1)''关闭文件。
赶上例外
MessageBox.Show("写入文件时出错。) + e.ToString())
最后
FileClose(1)
结束尝试
结束子
能帮我提供读取dvg整行列的代码吗?
Dim rowLine As String = ""
Try
FileOpen(1, fName, OpenMode.Output) '' Open file for output.
''WriteLine(1, "Hello", " ", "World") '' Separate strings with space.
''WriteLine(1, SPC(5), "5 leading spaces ") '' Print five leading spaces.
''WriteLine(1, TAB(10), "Hello") '' Print word at column 10.
''FileClose(1) '' Close file.
''sData = DataGridView1.Columns(1).Name.ToString()
''sData = Me.DataGridView1.CurrentRow.Cells(i).Value.ToStrin g
''MessageBox.Show(sData)
For j = 0 To (DataGridView2.Rows.Count - 2)
For I = 0 To (DataGridView2.Columns.Count - 1)
If Not TypeOf DataGridView2.CurrentRow.Cells.Item(I).Value Is DBNull Then
cellvalue = DataGridView2.CurrentRow.Cells.Item(I).Value.ToStr ing
Else
cellvalue = ""
End If
''DataGridView2.CurrentRow.Cells.Item(I).Value = cellvalue
rowLine = rowLine + cellvalue + ","
Next
WriteLine(1, TAB(0), rowLine) '' Print word at column 10.
''writer.WriteLine()
rowLine = ""
Next
FileClose(1) '' Close file.
Catch e As Exception
MessageBox.Show("Error occured while writing to the file." + e.ToString())
Finally
FileClose(1)
End Try
End Sub
Could you please help me giving the code that reads entire row column of the dvg?