Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/01/2009, 15:46
luise32
 
Fecha de Ingreso: noviembre-2006
Ubicación: Pasto
Mensajes: 154
Antigüedad: 18 años, 4 meses
Puntos: 1
Modifica archivo de excel

Hola, encontre este codigo en internet.... el cual permite "modificar" un archivo de excel especificando en q fila y en q colomna se desea colocar algun dato.

Private Sub btnAlmacenarExcel_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnAlmacenarExcel.Click

Dim oExcel As Excel.ApplicationClass
Dim oBooks As Excel.Workbooks
Dim oBook As Excel.WorkbookClass
Dim oSheet As Excel.Worksheet

' Inicia Excel y abre el workbook
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBooks = oExcel.Workbooks
oBook = oExcel.Workbooks.Add
oSheet = oBook.Sheets(1)

'oBook = oBooks.Open( _
' "C:\DevCare\DevCareExcelAutomation\Data.xls")

Const ROW_FIRST = 3
Dim iRow As Int64 = 1

' Encabezado
oSheet.Cells(ROW_FIRST, 1) = "ID"
oSheet.Cells(ROW_FIRST, 2) = "Compañía"
oSheet.Cells(ROW_FIRST, 3) = "Contacto"
oSheet.Cells(ROW_FIRST, 4) = "País"



oSheet.Cells(ROW_FIRST, 1).font.bold = True
oSheet.Cells(ROW_FIRST, 2).font.bold = True
oSheet.Cells(ROW_FIRST, 3).font.bold = True
oSheet.Cells(ROW_FIRST, 4).font.bold = True



oSheet.Columns(1).ColumnWidth = 10
oSheet.Columns(2).ColumnWidth = 40
oSheet.Columns(3).ColumnWidth = 30
oSheet.Columns(4).ColumnWidth = 15



' Loop que almacena los datos
'Dim rowCustomer As dsCustomers.CustomersRow
'For Each rowCustomer In Me.DsCustomers.Customers
'Dim iCurrRow As Int64 = ROW_FIRST + iRow
'oSheet.Cells(iCurrRow, 1) = rowCustomer.CustomerID
'oSheet.Cells(iCurrRow, 2) = rowCustomer.CompanyName
'oSheet.Cells(iCurrRow, 3) = rowCustomer.ContactName
'oSheet.Cells(iCurrRow, 4) = rowCustomer.Country



'iRow += 1
'Next



' Fórmula
oSheet.Cells(ROW_FIRST + iRow + 1, 1) = _
"=counta(R" & (ROW_FIRST + 1) & "C1:R" & _
(ROW_FIRST + iRow - 1).ToString & "C1)"







'' Cierra todo
'oBook.Close(True)
'System.Runtime.InteropServices.Marshal. _
' ReleaseComObject(oBook)
'oBook = Nothing



'System.Runtime.InteropServices.Marshal. _
' ReleaseComObject(oBooks)
'oBooks = Nothing



'oExcel.Quit()
'System.Runtime.InteropServices.Marshal. _
' ReleaseComObject(oExcel)
'oExcel = Nothing



End Sub

esto va en un boton... funciona casi al 100% lo unico malo es q no me modifica el archivo de esa ruta sino q me modifica un archivo nuevo q se crea... como puedo hacer para q el archivo q sea modificado sea el q abro y no el q abre excel con el nombre de Libro1.xlsx?

Agrego diciendo q los datos q se agregan en Libro1.xlsx son son ID Compañia Contacto Pais

Gracias!!!