Principales respuestas
Importar a excel por fechas

Pregunta
-
Buenos dias, soy nuevo en el tema estoy haciendo un programa en el cual se importan los datos, si se importan pero se importa toda la tabla de mysql, pero quisiera una condicion para importar por dia concreto,mes y año.
Mi codigo es el siguiente:
Imports Microsoft.Office.Interop
Public Class ExportaraExcel
Public Sub exportarexcel(ByVal tabla As DataTable)
Dim fila As DataRow
'Dim donde As String = "select * from jr_total where fecha_tot='" & D.bus_d.Text & "'"
Dim posicion As Integer = 4
Dim centro As String = Excel.XlHAlign.xlHAlignCenter
Dim MiExcel As New Excel.Application
Dim objlibroexcel As Excel.Workbook = MiExcel.Workbooks.Add
Dim objhojaexcel As Excel.Worksheet = objlibroexcel.Worksheets(1)
With objhojaexcel
.Visible = Excel.XlSheetVisibility.xlSheetVisible
.Activate()
.Name = "Exportacion"
.Range("A1").Value = "Datos de reporte"
.Range("A1").Font.Size = 28
.Range("A1").Font.Name = "Times New Roman"
.Range("A1").Font.Italic = True
.Range("A1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("A1").RowHeight = 30
.Range("A1:K1").MergeCells = True
.Range("A2").Value = "id_total"
.Range("A2").Font.Bold = True
.Range("A2").ColumnWidth = 15
.Range("A2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("B2").Value = "subtotal"
.Range("B2").ColumnWidth = 25
.Range("B2").Font.Bold = True
.Range("B2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("C2").Value = "IVA"
.Range("C2").ColumnWidth = 25
.Range("C2").Font.Bold = True
.Range("C2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("D2").Value = "total"
.Range("D2").ColumnWidth = 20
.Range("D2").Font.Bold = True
.Range("D2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("E2").Value = "fecha_tot"
.Range("E2").ColumnWidth = 15
.Range("E2").Font.Bold = True
.Range("E2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("F2").Value = "factura"
.Range("F2").ColumnWidth = 15
.Range("F2").Font.Bold = True
.Range("F2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("G2").Value = "hora_e"
.Range("G2").ColumnWidth = 15
.Range("G2").Font.Bold = True
.Range("G2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("H2").Value = "pago"
.Range("H2").ColumnWidth = 15
.Range("H2").Font.Bold = True
.Range("H2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("I2").Value = "Tipo_pago"
.Range("I2").ColumnWidth = 10
.Range("I2").Font.Bold = True
.Range("I2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("J2").Value = "Tipo_entrega"
.Range("J2").ColumnWidth = 10
.Range("J2").Font.Bold = True
.Range("J2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("K2").Value = "empleado"
.Range("K2").ColumnWidth = 10
.Range("K2").Font.Bold = True
.Range("K2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
End With
For Each fila In tabla.Rows
objhojaexcel.Range("A" & posicion).Value = fila.Item("id_total")
objhojaexcel.Range("B" & posicion).Value = fila.Item("subtotal")
objhojaexcel.Range("C" & posicion).Value = fila.Item("IVA")
objhojaexcel.Range("D" & posicion).Value = fila.Item("total")
objhojaexcel.Range("E" & posicion).Value = fila.Item("fecha_tot")
objhojaexcel.Range("F" & posicion).Value = fila.Item("factura")
objhojaexcel.Range("G" & posicion).Value = fila.Item("hora_e")
objhojaexcel.Range("H" & posicion).Value = fila.Item("pago")
objhojaexcel.Range("I" & posicion).Value = fila.Item("Tipo_pago")
objhojaexcel.Range("J" & posicion).Value = fila.Item("Tipo_entrega")
objhojaexcel.Range("K" & posicion).Value = fila.Item("empleado")
'objhojaexcel.Range("K" & posicion).Value = fila.Item("total")
posicion += 1
Next
MiExcel.Visible = True
End Sub
End Class
Respuestas
-
Yo te diría que no intentes cambiar el código que has mostrado, que lo que hace es que recibe un DataTable y luego exporta ese DataTable a Excel. En su lugar, retrocede hasta el sitio en donde cargas ese DataTable a partir de MySql, y mete ahí el filtro. Simplemente, pon un "where" en la sentencia SQL que usas para cargar los datos, de manera que solo se traigan desde la base de datos los registros que quieres exportar.
- Propuesto como respuesta Pablo Rubio jueves, 23 de enero de 2020 16:33
- Marcado como respuesta kyoraul jueves, 23 de enero de 2020 22:46
Todas las respuestas
-
Yo te diría que no intentes cambiar el código que has mostrado, que lo que hace es que recibe un DataTable y luego exporta ese DataTable a Excel. En su lugar, retrocede hasta el sitio en donde cargas ese DataTable a partir de MySql, y mete ahí el filtro. Simplemente, pon un "where" en la sentencia SQL que usas para cargar los datos, de manera que solo se traigan desde la base de datos los registros que quieres exportar.
- Propuesto como respuesta Pablo Rubio jueves, 23 de enero de 2020 16:33
- Marcado como respuesta kyoraul jueves, 23 de enero de 2020 22:46
-