new text document (6)

Download New Text Document (6)

If you can't read please download the document

Upload: michelle-brown

Post on 16-Sep-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Imports System.Data.OleDbPublic 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 PieseDataGridView.Columns.Count - 1 xlWS.cells(1, c + 1).value = PieseDataGridView.Columns(c).HeaderText Next 'exportamos las cabeceras de columnas For r As Integer = 0 To PieseDataGridView.RowCount - 1 For c As Integer = 0 To PieseDataGridView.Columns.Count - 1 xlWS.cells(r + 2, c + 1).value = PieseDataGridView.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 PieseBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PieseBindingNavigatorSaveItem.Click Me.Validate() Me.PieseBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.Database1DataSet) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'Database1DataSet.piese' table. You can move, or remove it, as needed. Me.PieseTableAdapter.Fill(Me.Database1DataSet.piese) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim save As New SaveFileDialog save.Filter = "Archivo Excel | *.xlsx" If save.ShowDialog = Windows.Forms.DialogResult.OK Then Exportar_Excel(Me.PieseDataGridView, save.FileName) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 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.PieseDataGridView.Columns.Clear() Me.PieseDataGridView.DataSource = Dt Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Error") End Try End SubEnd Class