Exportar de datagridview a TXT en Visual Basic .
NET Function gridatxt(ByVal Grid As DataGridView) As Boolean Dim texto As StreamWriter Dim escribo As String Dim filas As Integer Dim columnas As Integer Dim titulo As String = "" Dim palabra As String Dim letras As Integer Dim nuevo As String
filas = Grid.RowCount - 1 columnas = Grid.ColumnCount - 1
texto = New StreamWriter("C:"PRUEBA".txt"
Dim tamao(columnas) As Integer For i = 0 To columnas tamao(i) = 0 Next For a = 0 To filas Grid.CurrentCell = Grid.Rows(a).Cells(0) For b = 0 To columnas titulo = Grid.Columns(b).Name If IsDBNull(Grid.CurrentRow.Cells.Item(titulo).Value) Then palabra = "NULL" Else palabra = Grid.CurrentRow.Cells.Item(titulo).Value End If
letras = palabra.Length If letras > tamao(b) Then tamao(b) = letras End If Next Next
For a = 0 To filas escribo = "" Grid.CurrentCell = Grid.Rows(a).Cells(0) For b = 0 To columnas titulo = Grid.Columns(b).Name If b = 0 Then If IsDBNull(Grid.CurrentRow.Cells.Item(titulo).Value) Then escribo = "NULL" Else escribo = Grid.CurrentRow.Cells.Item(titulo).Value letras = escribo.Length End If
Else
If IsDBNull(Grid.CurrentRow.Cells.Item(titulo).Value) Then escribo = "NULL" Else nuevo = Grid.CurrentRow.Cells.Item(titulo).Value letras = nuevo.Length Do While letras < tamao(b) nuevo = nuevo & " "
letras = letras + 1 Loop escribo = escribo & " " & nuevo End If End If Next texto.WriteLine(escribo) Next texto.Close() End Function Cargar un DatagridView con un archivo de texto Delimitado por Comas
private void btnExaminar_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ;
if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtDirFile.Text = openFileDialog1.FileName;
} }
private void btnCargar_Click(object sender, EventArgs e) { if (txtDirFile.Text != "") { string texto; int count = 4; string[] split = null;
try { StreamReader tr = new StreamReader(txtDirFile.Text); while ((texto = tr.ReadLine()) != null) { //this.textBoxPwd.Text += texto; split = texto.Split(new Char[] { ,, :, ; }, count); dgvDatos.Rows.Add(split[0], split[1], split[2], split[3]); split = null;
} MessageBox.Show("Le archivo se cargo correctamente", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
} catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Seleccione un archivo y un delimitador", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
De dgw a txt Function Guardar(ByVal Grid As DataGridView) As Boolean Dim texto As StreamWriter Dim escribo As String Dim filas As Integer Dim columnas As Integer Dim titulo As String = "" Dim i As Integer Dim a As Integer Dim b As Integer Dim palabra As String
Dim letras As Integer Dim nuevo As String
filas = Grid.RowCount - 1 columnas = Grid.ColumnCount - 1
Dim direccion As String = "" direccion = "C:\datos2.txt" texto = New StreamWriter(direccion) Dim tamao(columnas) As Integer For i = 1 To columnas tamao(i) = 1 Next For a = 1 To filas Grid.CurrentCell = Grid.Rows(a).Cells(1) For b = 2 To columnas titulo = Grid.Columns(b).Name If IsDBNull(Grid.CurrentRow.Cells.Item(titulo).Value) Then palabra = "NULL" Else palabra = Grid.CurrentRow.Cells.Item(titulo).Value End If
letras = palabra.Length If letras > tamao(b) Then tamao(b) = letras End If
Next Next
For a = 0 To filas escribo = "" Grid.CurrentCell = Grid.Rows(a).Cells(1) For b = 1 To columnas titulo = Grid.Columns(b).Name If b = 1 Then If IsDBNull(Grid.CurrentRow.Cells.Item(titulo).Value) Then escribo = "NULL" Else escribo = Grid.CurrentRow.Cells.Item(titulo).Value letras = escribo.Length End If
Else If String.IsNullOrEmpty(Grid.CurrentRow.Cells.Item(titulo).Value.ToString()) Then escribo = "NULL" Else
nuevo = Grid.CurrentRow.Cells.Item(titulo).Value letras = nuevo.Length
nuevo = "|" & nuevo
escribo = escribo & nuevo
End If End If
Next texto.WriteLine(escribo) Next texto.Close() End Function End Class