Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.Odbc
Imports Excel
Imports System.Reflection
'Los imports los declaras arriba, tienes q tener un datagrid con el nombre dgDatosPush el cual contenga datos y un boton btnExcel el siguiente es el codigo:
Private Sub btnExportar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportar.Click
Try
If dgDatosPush.Rows.Count = 0 Then
MsgBox("No hay datos") : Exit Sub
Else
Dim ExcelApp As New Excel.Application()
Dim TheSheet As Excel.Worksheet
ExcelApp.Visible = False
' Add a Worksheet to Excel
Dim theWorkbook As Excel.Workbook = ExcelApp.Workbooks.Add() 'ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
TheSheet = DirectCast(theWorkbook.Worksheets(1), Excel.Worksheet)
Dim iColumnas As Integer = 0
'For ICol As Integer = 0 To dgDatosPush.Columns.Count - 1
' TheSheet.Cells(1, ICol + 2) = dgDatosPush.Columns(ICol).HeaderText
'Next
TheSheet.Name = "Atributo_Push"
dgDatosPush.MultiSelect = True
dgDatosPush.SelectAll()
If dgDatosPush.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
Clipboard.SetDataObject(dgDatosPush.GetClipboardContent())
dgDatosPush.ClearSelection()
dgDatosPush.MultiSelect = False
End If
TheSheet.Columns("P:P").NumberFormat = "@"
TheSheet.Range("A2").Select()
TheSheet.Paste(Missing.Value, Missing.Value)
Clipboard.Clear()
With TheSheet.Cells
.ShrinkToFit = False
.WrapText = False
.EntireColumn.AutoFit()
End With
ExcelApp.Visible = True
' Btnexcel.Enabled = False
End If
Catch x As Exception
MessageBox.Show(x.Message & " " & x.ToString, "error : ")
End Try
End Sub
La información y el conocimiento deben ser compartidos sin necesidad de lucrarse por hacerlo. Software, descargas y novedades. Las mejores plantillas web, con los trucos más útiles.
miércoles, 25 de enero de 2012
exportar a excel desde visualbasic.net
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario