Ver Mensaje Individual
  #2 (permalink)  
Antiguo 08/08/2011, 17:39
Avatar de moradazo
moradazo
 
Fecha de Ingreso: julio-2008
Ubicación: [email protected]
Mensajes: 355
Antigüedad: 16 años, 8 meses
Puntos: 2
Respuesta: importar y exportar a Excel : Visual Basic 2005

Hola a todos, al menos pude ya lograr importar a mi datagrid y expotar a excel. El único detalle es que no puedo agregar filas o "rows" ya que el datagrid se encuentra enlazado por haber importado un archivo de excel, y no me deja ingresar nuevas filas de ningún modo:



Por ejemplo he intentado con un While pero no funciona:

DataGridView1.Rows(i).Cells(0).Value = Textbox1.Text

O con el agregar normal:

DataGridView1.Rows.Add()

Alguien me puede dar una mano en como agregar filas nuevas con el datagrid enlazado?

Gracias

Aqui el código de importar y exportar:

Código vb:
Ver original
  1. Imports System.Data.SqlClient
  2. Imports System.Data.OleDb
  3. Public Class Form1
  4.     Inherits System.Windows.Forms.Form
  5.     Private MiDataSet As New DataSet()
  6.     Private MiEnlazador As New BindingSource
  7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8.         Dim abrir As New OpenFileDialog
  9.         abrir.Filter = "Archivo de excel|*.xls"
  10.         If abrir.ShowDialog = Windows.Forms.DialogResult.OK Then
  11.             importexcel(abrir.FileName)
  12.         End If
  13.     End Sub
  14.     Private Sub importexcel(ByVal path As String)
  15.         Dim sconnectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
  16.         & "Data Source=" + path + "" _
  17.         & ";" & "Extended Properties=Excel 8.0;"
  18.         Dim objconn As New OleDb.OleDbConnection(sconnectionstring)
  19.         objconn.Open()
  20.         Dim objcmdselect As New OleDb.OleDbCommand("SELECT * FROM [Hoja1$]", objconn)
  21.         Dim objadapter1 As New OleDb.OleDbDataAdapter()
  22.         objadapter1.SelectCommand = objcmdselect
  23.         Dim objdataset1 As New DataSet()
  24.         objadapter1.Fill(objdataset1, "XLData")
  25.         Me.DataGridView1.DataSource = objdataset1.Tables(0).DefaultView
  26.         objconn.Close()
  27.     End Sub
  28.  
  29.     Public Sub exportaraexcel(ByVal dgv As DataGridView, ByVal pth As String)
  30.         Dim xlapp As Object = createobject("Excel.Application")
  31.         Dim xlwb As Object = xlapp.workbooks.add
  32.         Dim xlws As Object = xlwb.Worksheets(1)
  33.  
  34.         For C As Integer = 0 To datagridview1.columns.count - 1
  35.             xlws.cells(1, c + 1).value = Datagridview1.columns(c).headertext
  36.         Next
  37.         For r As Integer = 0 To datagridview1.rowcount - 1
  38.             For c As Integer = 0 To datagridview1.columns.count - 1
  39.                 xlws.cells(r + 2, c + 1).Value = datagridview1.item(c, r).Value
  40.             Next
  41.         Next
  42.  
  43.         xlwb.saveas(pth)
  44.         xlws = Nothing
  45.         xlwb = Nothing
  46.         xlapp.quit()
  47.         xlapp = Nothing
  48.     End Sub
  49.  
  50.  
  51.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  52.         Dim save As New SaveFileDialog
  53.         save.Filter = "Archivo Excel | *.xls"
  54.  
  55.         If save.ShowDialog = Windows.Forms.DialogResult.OK Then
  56.             exportaraexcel(Me.DataGridView1, save.FileName)
  57.         End If
  58.     End Sub