You are on page 1of 13

Filtrado por todos los mensajes

Vistas:

arbol

Ayuda para exportar datos de datagrid a excel Veronica (05/11/2009 15:03:39) 4.480 visitas 6 respuestas RE:Ayuda para exportar datos de datagrid a excel MIRIAM (05/11/2009 18:45:52) RE:Ayuda para exportar datos de datagrid a excel gon (05/11/2009 18:54:47) RE:Ayuda para exportar datos de datagrid a excel Veronica (06/11/2009 13:50:26) RE:Ayuda para exportar datos de datagrid a excel Alejandro (06/11/2009 17:43:51) RE:Ayuda para exportar datos de datagrid a excel Veronica (11/11/2009 02:58:56) RE:Ayuda para exportar datos de datagrid a excel Nelly (24/12/2009 18:13:56)

Asunto:

Ayuda para exportar datos de datagrid a excel

Autor: Veronica (28 intervenciones) Fecha: 05/11/2009 15:03:39 Hola a todos: Estoy creando una aplicacin en VB 2005 y necesito importar los datos de un datagridview a excel; es importante llevar los encabezados de cada columna y luego las filas con sus respectivos datos. Alguien podra ayudarme con esto?...podran proporcionarme algn tipo de cdigo q me ayudara? Muchas gracias Tambin te puede interesar... - Cursos de Visual Basic.NET - Temas de Visual Basic.NET - Cdigo fuente de Visual Basic.NET

Asunto:

RE:Ayuda para exportar datos de datagrid a excel

Autor: MIRIAM (2 intervenciones) Fecha: 05/11/2009 18:45:52 Hola Veronica, tengo uno muy bueno espero te sirva, lo tengo para office 2007 , este lo tome de las aportaciones del guille.

si tienes duda me dices 'incluyes las referencias microsoft excel 12.0 object library microsoft office 12.0 object library 'incluyes este -> Imports Microsoft.Office.Interop

'dgq <- es el nombre de mi data grid Dim fila, columna, f, c, c1, f1 As Integer fila = dgq.RowCount columna = dgq.ColumnCount 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 = 1

Dim iRow As Int64 = 1 ' Encabezado oSheet.Cells(ROW_FIRST, 1) = "No_Caso" oSheet.Cells(ROW_FIRST, 2) = "Atraso(Dias)" oSheet.Cells(ROW_FIRST, 3) = "Tipo_Queja" oSheet.Cells(ROW_FIRST, 4) = "Servicio" oSheet.Cells(ROW_FIRST, 5) = "Queja" oSheet.Cells(ROW_FIRST, 6) = "Status" oSheet.Cells(ROW_FIRST, 7) = "Llamada_E" oSheet.Cells(ROW_FIRST, 8) = "Llamada_S" oSheet.Cells(ROW_FIRST, 9) = "Contacto" oSheet.Cells(ROW_FIRST, 10) = "Cliente" oSheet.Cells(ROW_FIRST, 11) = "Usuario" oSheet.Cells(ROW_FIRST, 12) = "F_Alta" oSheet.Cells(ROW_FIRST, 13) = "F_Modificacion" 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.Cells(ROW_FIRST, 5).font.bold = True oSheet.Cells(ROW_FIRST, 6).font.bold = True oSheet.Cells(ROW_FIRST, 7).font.bold = True oSheet.Cells(ROW_FIRST, 8).font.bold = True oSheet.Cells(ROW_FIRST, 9).font.bold = True oSheet.Cells(ROW_FIRST, 10).font.bold = True oSheet.Cells(ROW_FIRST, 11).font.bold = True oSheet.Cells(ROW_FIRST, 12).font.bold = True oSheet.Cells(ROW_FIRST, 13).font.bold = True

oSheet.Columns(1).ColumnWidth = 10 oSheet.Columns(2).ColumnWidth = 40 oSheet.Columns(3).ColumnWidth = 30 oSheet.Columns(4).ColumnWidth = 15 oSheet.Columns(5).ColumnWidth = 40 oSheet.Columns(6).ColumnWidth = 15 oSheet.Columns(7).ColumnWidth = 40 ' Loop que almacena los datos 'Dim rowCustomer As dsCustomers.CustomersRow f1 = 0 For f = 2 To fila c1 = 1 For c = 0 To columna - 1 oSheet.Cells(f, c1) = dgq(c, f1).Value c1 = c1 + 1 Next c f1 = f1 + 1 Next f oSheet.Cells.Select() oSheet.Cells.EntireColumn.AutoFit() oSheet.Columns(5).ColumnWidth = 30

Asunto:

RE:Ayuda para exportar datos de datagrid a excel

Autor: gon (31 intervenciones)

Fecha: 05/11/2009 18:54:47 hola, te paso otro ejemplo Sub ExportarAExcel(ByVal xgridexpo As DataGridView) Dim strStreamW As Stream Dim strStreamWriter As StreamWriter Dim Filas = xgridexpo.Rows.Count Dim Columnas = xgridexpo.Columns.Count Dim Archivo As String = Application.StartupPath & "\Centros.csv" Dim Linea As String = "" Dim f As Integer Dim c As Integer File.Delete(Archivo) strStreamW = File.OpenWrite(Archivo) strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.UTF8) 'CABECERA For c = 0 To Columnas - 4 Linea = Linea & xgridexpo.Columns(c).Name & ";" Next Linea = Mid(Linea, 1, Linea.ToString.Length - 1) strStreamWriter.WriteLine(Linea) Linea = Nothing

'FILAS For f = 0 To Filas - 1 For c = 0 To Columnas - 4 Linea = Linea & xgridexpo.Item(c, f).Value & ";" 'MsgBox(Grid.Item(c, f).Value) Next Linea = Mid(Linea, 1, Linea.ToString.Length - 1) strStreamWriter.WriteLine(Linea) Linea = Nothing Next strStreamWriter.Close() Try Process.Start(Archivo) 'Ejecuta el archivo creado Catch Ex As Exception MsgBox(Ex.Message, MsgBoxStyle.Critical, Ex.Source) End Try aEXCEL = False End Sub

Asunto:

RE:Ayuda para exportar datos de datagrid a excel

Autor: Veronica (28 intervenciones) Fecha: 06/11/2009 13:50:26

Muchas gracias a ambos!...ahora mismo pongo manos a la obra y les cuento q salio. Saludos!

Asunto:

RE:Ayuda para exportar datos de datagrid a excel

Autor: Alejandro (15 intervenciones) Fecha: 06/11/2009 17:43:51 Que tal, te paso un codigo que a mi me ayudo bastante, es facil de entender. Public Function GridAExcel() '***** PRUEBAS PARA MANDAR UN DGV A EXCEL : ALEX ***** 'Creamos las variables Dim exApp As New Excel.Application 'Se debe agregar la referencia en el proyecto para usar esta funcin Dim exLibro As Excel.Workbook Dim exHoja As Excel.Worksheet ' Variables para # de columnas y # de renglones Dim iCol As Integer Dim iRow As Integer Try 'Aadimos el Libro al programa, y la hoja al libro

exLibro = exApp.Workbooks.Add exHoja = exLibro.Worksheets.Add() iCol = Go_Grid.Columns.Count 'MiDato2 iRow = Go_Grid.Rows.Count 'MiDato1 If iCol = 0 Then MsgBox("No hay informacin por exportar a Excel", MsgBoxStyle.Information, GsTituloMsg) Else 'Aqui recorremos todas las filas For i As Integer = 1 To iCol If Go_Grid.Columns(i - 1).Visible = True Then exHoja.Cells.Item(1, i) = Go_Grid.Columns(i 1).Name.ToString exHoja.Cells.Item(1, i).HorizontalAlignment = 3 End If Next 'Por cada fila pone todas las columnas y vamos escribiendo. For Fila As Integer = 0 To iRow - 1 For Col As Integer = 0 To iCol - 1 If Go_Grid.Columns(Col).Visible = True Then exHoja.Cells.Item(1, Col + 1) = Go_Grid.Columns(Col).HeaderText 'Estas son las columnas del encabezado exHoja.Cells.Item(Fila + 2, Col + 1) = Go_Grid.Rows(Fila).Cells(Col).Value End If Next Next

'Titulo en negrita, Alineado al centro y que el tamao de la columna se ajuste al texto exHoja.Rows.Item(1).Font.Bold = 1 exHoja.Rows.Item(1).HorizontalAlignment = 3 exHoja.Columns.AutoFit() 'Aplicacin visible exApp.Application.Visible = True End If 'Se eliminan las variables de objeto excel exApp = Nothing exHoja = Nothing exLibro = Nothing Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, "Error al exportar a Excel") Return False End Try Return True End Function Espero te sirva, saludos.

0
Asunto: RE:Ayuda para exportar datos de datagrid a excel Autor: Veronica (28 intervenciones) Fecha: 11/11/2009 02:58:56 Hola a todos: Desde ya MUCHAS GRACIAS a quienes me han dado una mano enorme con estos cdigos para poder exportar a Excel el contenido de un datagridview. Una duda ms; desde q utilic el cdigo, empec a tener problemas con el filtrado de datos en el datagridview. Por ejemplo, cuando coloco la referencia Mocrosoft.Office.Interop.Excel automticamente me seala en azul el criterio de filtrado dicciendome q son demasiado argumentos para la instruccin Select. Y si por caso se me ocurre quitar la referencia, ese error no lo tengo pero no me deja filtrar tampoco. Alguien tiene idea q puede ser?....antes no suceda esto y la verdad q me cans de darle vueltas. Este es el cdigo q utilizo para el filtrado: Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim mitabla As DataTable =

Me.ContactosDataSet.Contactos Dim filas() As DataRow filas = mitabla.Select("Apellido LIKE '" & TextBox1.Text & "%'") If filas.Length > 0 Then dtgContactos.DataSource = filas dtgContactos.Refresh() Me.ContactosDataSet.Clear() End If End Sub

Asunto:

RE:Ayuda para exportar datos de datagrid a excel

Autor: Nelly (1 intervencin) Fecha: 24/12/2009 18:13:56 A mi el filtro en una tabla me sale de esta manera es cosa de que adaptes tu codigo.... Public Sub Buscar(ByVal nomObra As String, ByVal Folio As String, ByVal Equipo As String, ByVal Personal As String) Sqlconnection1 = New SqlConnection(conexion) cmd = New SqlCommand cmd.CommandText = "SELECT * FROM Obra " cmd.Connection = Sqlconnection1

Sqlconnection1.Open() cmd.ExecuteNonQuery() Sqlconnection1.Close() da = New SqlDataAdapter(cmd) ds = New DataSet() da.Fill(ds) vista = ds.Tables(0).DefaultView DataGridView1.DataSource = vista vista.RowFilter = "nomObra LIKE '%" & nomObra & "%'" End Sub

You might also like