EXPORTAR DATAGRIDVIEW A EXCEL

Private Sub btEXCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEXCEL.Click



   Dim obj_Excel As Object
   Dim obj_hoja As Object
   Dim obj_libro As Object
   Dim LETEXCEL() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"}


   obj_Excel = CreateObject("Excel.Application")
   obj_libro = obj_Excel.Workbooks.Add()
   obj_hoja = obj_libro.Worksheets(1)


   Dim i As Integer


   For i = 0 To dtg.Columns.Count - 1
      obj_hoja.Range(LETEXCEL(i) & "1").Value = dtg.Columns(i).HeaderText
   Next


   'Ponemos en negrita los encabezados
   obj_hoja.Range("A1:N1").Font.Bold = True


   Dim j As Integer
   'Recorremos el datagridview (dtg) i exportamos celda a celda
   For i = 0 To dtg.Columns.Count - 1
      For j = 0 To dtg.RowCount - 1
        obj_hoja.range(LETEXCEL(i) & j + 2).value = dtg(i, j).Value
      Next
   Next


  obj_Excel.Visible = True
End Sub

7 comentarios:

  1. vos que es esa cosa de "dtg"?? xq me dice q no esta declarado

    ResponderEliminar
    Respuestas
    1. dtg es el nombre del Datagridview, del cual quieres exportar los datos a excel.

      Eliminar
  2. Hola no me funciona tengo un error al llenar la hoja me dice
    No se controló System.NullReferenceException
    Message=Referencia a objeto no establecida como instancia de un objeto.
    Source=CarpetaBancos
    StackTrace:
    en CarpetaBancos.loads.dtgtoexcel(DataGridView dtg) en D:\Desarrollos\CarpetaBancos\CarpetaBancos\loads.vb:línea 255
    en CarpetaBancos.frmBancos.btnToexcel_Click(Object sender, EventArgs e) en D:\Desarrollos\CarpetaBancos\CarpetaBancos\frmBancos.vb:línea 374
    en System.Windows.Forms.Control.OnClick(EventArgs e)
    en System.Windows.Forms.Button.OnClick(EventArgs e)
    en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    en System.Windows.Forms.Control.WndProc(Message& m)
    en System.Windows.Forms.ButtonBase.WndProc(Message& m)
    en System.Windows.Forms.Button.WndProc(Message& m)
    en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    en CarpetaBancos.My.MyApplication.Main(String[] Args) en 17d14f5c-a337-4978-8281-53493378c1071.vb:línea 81
    en System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    en System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    ResponderEliminar
  3. Excelente ayuda. Mis agradecimientos

    ResponderEliminar