I have a project of loading an excel file to a DataGridView and then save that DataGridView data to a ".ini" file. So, Since I am very new to VB.net i would like your help.
This is my code. I have managed to read the Excel file in a DataGridView and save it as text file. I can't understand from the tutorials how to save it as a ini file. I need help... Thank all of you.
Imports System
Imports System.IO
Imports Microsoft.Win32
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "password" Then
Form2.Show()
Else
MessageBox.Show("Please Enter The Correct Password", "Information")
End If
Try
Dim MyConnection As OleDb.OleDbConnection
Dim Ds As System.Data.DataSet
Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Pratto_lp3\Documents\Visual Studio 2015\SCHEDULE.xlsx';Extended Properties='Excel 12.0 Xml;HDR=YES;'")
MyAdapter = New System.Data.OleDb.OleDbDataAdapter("Select * from [S-BOTTOM_(1)$]", MyConnection)
Ds = New System.Data.DataSet
MyAdapter.Fill(Ds)
Me.DataGridView1.DataSource = Ds.Tables(0)
Catch ex As Exception
'MsgBox(ex.Message)
MsgBox("ExcelFile File Loaded")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim writer As TextWriter = New StreamWriter("c:\users\pratto_lp3\documents\visual studio 2015\projects\DtgrdToText.ini")
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString & vbTab & "|")
Next
writer.WriteLine("")
writer.WriteLine("....................................")
Next
writer.Close()
MessageBox.Show("Data Saved")
'Dim result As String = ""
''go through all rows
'For rownumber As Integer = 0 To DataGridView1.Rows.Count - 1
' 'this gets just column 0 (the first column)
' result &= DataGridView1.Rows(rownumber).Cells("f10").Value & vbCrLf
' 'result += datagridview1.item(0, rownumber).tostring
'Next
''write out the string
'File.WriteAllText("c:\users\pratto_lp3\documents\visual studio 2015\projects\datagrid1", result)
'MessageBox.Show("file saved")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
try this:
Dim writer As System.IO.StreamWriter
writer = My.Computer.FileSystem.OpenTextFileWriter("c:\users\pratto_lp3\documents\visual studio 2015\projects\DtgrdToText.ini", True)
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString & vbTab & "|")
Next
writer.WriteLine("")
writer.WriteLine("....................................")
Next
writer.Close()
MessageBox.Show("Data Saved")
the datagridview is bidimensional, have row and colums. your result must be:
[row1]
col1=some data
col2=soma data
[row2]
col1=some data1
col2=soma data2
this is your aim?
ok, try this:
Dim writer As System.IO.StreamWriter
writer = My.Computer.FileSystem.OpenTextFileWriter("c:\users\pratto_lp3\documents\visual studio 2015\projects\DtgrdToText.ini", True)
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
'write row
writer.WriteLine(String.Format("[row{0}]", i))
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
'write columns title and value
writer.WriteLine(String.Format("{0}={1}", DataGridView1.Columns(j).Name, DataGridView1.Rows(i).Cells(j).Value.ToString))
Next
writer.WriteLine(String.Empty)
Next
writer.Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.