0% found this document useful (0 votes)
75 views3 pages

Libreria:: 'Exportamos Los Caracteres de Las Columnas

This document contains code for importing data from an Excel file into a DataGridView in Visual Basic. It includes a method to export data from a DataGridView to an Excel file, and a button click event handler to open a file dialog and import the data from the selected Excel file into a DataTable, which is then set as the data source for the DataGridView.

Uploaded by

Belkiss Caceres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views3 pages

Libreria:: 'Exportamos Los Caracteres de Las Columnas

This document contains code for importing data from an Excel file into a DataGridView in Visual Basic. It includes a method to export data from a DataGridView to an Excel file, and a button click event handler to open a file dialog and import the data from the selected Excel file into a DataTable, which is then set as the data source for the DataGridView.

Uploaded by

Belkiss Caceres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Libreria:

Imports System.Data.OleDb

Public Class Form1

Public Sub Exportar_Excel(ByVal dgv As DataGridView, ByVal pth As String)

Dim xlApp As Object = CreateObject("Excel.Application")

'crear una nueva hoja de calculo

Dim xlWB As Object = xlApp.WorkBooks.add

Dim xlWS As Object = xlWB.WorkSheets(1)

'exportamos los caracteres de las columnas

For c As Integer = 0 To EMPLEADOSDataGridView.Columns.Count - 1

xlWS.cells(1, c + 1).value = EMPLEADOSDataGridView.Columns(c).HeaderText

Next

'exportamos las cabeceras de columnas

For r As Integer = 0 To EMPLEADOSDataGridView.RowCount - 1

For c As Integer = 0 To EMPLEADOSDataGridView.Columns.Count - 1

xlWS.cells(r + 2, c + 1).value = EMPLEADOSDataGridView.Item(c, r).Value

Next

Next

'guardamos la hoja de calculo en la ruta especificada

xlWB.saveas(pth)

xlWS = Nothing

xlWB = Nothing

xlApp.quit()

xlApp = Nothing
End Sub

Private Sub GenerarEXCEL_Click(sender As Object, e As EventArgs) Handles


GenerarEXCEL.Click

Dim save As New SaveFileDialog

save.Filter = "Archivo Excel | *.xlsx"

If save.ShowDialog = Windows.Forms.DialogResult.OK Then

Exportar_Excel(EMPLEADOSDataGridView, save.FileName)

End If

End Sub

Codigo de boton

Private Sub Button1_Click(sender As Object, e As EventArgs)

Dim stRuta As String = ""

Dim openFD As New OpenFileDialog

With openFD

.Title = "Seleccionar archivos"

.Filter = "Archivos Excel(*.xls;*.xlsx)|*.xls;*xlsx|Todos los archivos(*.*)|*.*"

.Multiselect = False

.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop

If .ShowDialog = Windows.Forms.DialogResult.OK Then

stRuta = .FileName

End If

End With

Try

Dim stConexion As String = ("Provider=Microsoft.ACE.OLEDB.12.0;" & ("Data Source=" &


(stRuta & ";Extended Properties=""Excel 12.0;Xml;HDR=YES;IMEX=2"";")))

Dim cnConex As New OleDbConnection(stConexion)


Dim Cmd As New OleDbCommand("Select * From [Hoja1$]")

Dim Ds As New DataSet

Dim Da As New OleDbDataAdapter

Dim Dt As New DataTable

cnConex.Open()

Cmd.Connection = cnConex

Da.SelectCommand = Cmd

Da.Fill(Ds)

Dt = Ds.Tables(0)

Me.EMPLEADOSDataGridView.DataSource = Dt

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")

End Try

End Sub

End Class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy