Foros del Web » Programación para mayores de 30 ;) » .NET »

copiar recordset en excel

Estas en el tema de copiar recordset en excel en el foro de .NET en Foros del Web. Hola a todos, tengo un problema, lo que ocurre es que me gustaria poder exportar datos de sql a excel desde visual basic 2010, es ...
  #1 (permalink)  
Antiguo 21/10/2014, 17:02
 
Fecha de Ingreso: octubre-2008
Mensajes: 268
Antigüedad: 15 años, 7 meses
Puntos: 3
copiar recordset en excel

Hola a todos, tengo un problema, lo que ocurre es que me gustaria poder exportar datos de sql a excel desde visual basic 2010, es .net

Por medio de macro ya lo he conseguido guardando toda la informacion en un resultset y despues en un objeto de excel pongo nomObjeto.Copyrecordset rst y se copia todo el resultado sin necesidad de llenar cada celda una por una.

Alguie q me ayude? Graciass.
  #2 (permalink)  
Antiguo 22/10/2014, 01:02
 
Fecha de Ingreso: noviembre-2002
Ubicación: DF
Mensajes: 1.056
Antigüedad: 21 años, 5 meses
Puntos: 37
Respuesta: copiar recordset en excel

En mi aplicacion (web) yo pongo un boton que dice "exportar a excel", cuando clickean se ABRE EL EXCEL conteniendo la info del dataset (requiere q el equipo donde se visualiza la aplicacion tenga excel instalado)
(los Response. {mso-number-format:General; los obtuve grabando desde excel primero un archivo para que se respetara el formato como si fuera una plantilla)
:

Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=inscripciones.xls")




strSql = "SELECT campos FROM TABLA WHERE empresa=" & empresa
rsTemporal = objRutinasDB.recuperarRecordsetGENERICO(strSql)

'Response.Write("<html xmlns:o=3D""urn:schemas-microsoft-com:office:office""")
Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office'" + Environment.NewLine)
'Response.Write("xmlns:x=3D""urn:schemas-microsoft-com:office:excel""")
Response.Write("xmlns:x='urn:schemas-microsoft-com:office:excel'" + Environment.NewLine)
'Response.Write("xmlns=3D""http://www.w3.org/TR/REC-html40"">")
Response.Write("xmlns='http://www.w3.org/TR/REC-html40'>" + Environment.NewLine)



Response.Write("<head>")
' Response.Write("")
Response.Write("<style>" + Environment.NewLine)


Response.Write("@page")
Response.Write("{margin:.5in .33in .35in .26in;" + Environment.NewLine)
Response.Write("mso-header-margin:.05in;" + Environment.NewLine)
Response.Write("mso-footer-margin:0in;" + Environment.NewLine)
Response.Write("mso-page-orientation:landscape;}" + Environment.NewLine)


Response.Write("td")

'{mso-page-orientation:landscape;}
'Response.Write("{mso-style-parent:style0;")
Response.Write("{mso-page-orientation:landscape;mso-style-parent:style0;")

Response.Write("padding:0px;")
Response.Write("mso-ignore:padding;")
Response.Write("color:windowtext;")
Response.Write("font-size:8.0pt;")
Response.Write("font-weight:400;")
Response.Write("font-style:normal;")
Response.Write("text-decoration:none;")
Response.Write("font-family:Arial;")
Response.Write("mso-generic-font-family:auto;")
Response.Write("mso-font-charset:0;")
Response.Write("mso-number-format:General;")
Response.Write("text-align:general;")
Response.Write("vertical-align:bottom;")
Response.Write("border:none;")
Response.Write("mso-background-source:auto;")
Response.Write("mso-pattern:auto;")
Response.Write("mso-protection:locked visible;")
Response.Write("white-space:nowrap;")
Response.Write("mso-rotate:0;}")


Response.Write(".style0")
Response.Write("{mso-number-format:General;")
Response.Write("text-align:general;")
Response.Write("vertical-align:bottom;")
Response.Write("white-space:nowrap;")
Response.Write("mso-rotate:0;")
Response.Write("mso-background-source:auto;")
Response.Write("mso-pattern:auto;")
Response.Write("color:windowtext;")
Response.Write("font-size:8.0pt;")
Response.Write("font-weight:400;")
Response.Write("font-style:normal;")
Response.Write("text-decoration:none;")
Response.Write("font-family:Arial;")
Response.Write("mso-generic-font-family:auto;")
Response.Write("mso-font-charset:0;")
Response.Write("border:none;")
Response.Write("mso-protection:locked visible;")
Response.Write("mso-style-name:Normal;")
Response.Write("mso-style-id:0;}")

Response.Write(".xl110")
Response.Write("{mso-style-parent:style0;")
'Response.Write("mso-number-format:'MM:SS.000';")
Response.Write("font-size:6.0pt;")
Response.Write("font-family:'Arial Narrow', sans-serif;")
Response.Write("mso-font-charset:0;}")

Response.Write(".estiloMini")
Response.Write("{mso-style-parent:style0;")
Response.Write("font-size:5.0pt;")
Response.Write("font-family:'Arial Narrow', sans-serif;")
Response.Write("mso-font-charset:0;}")

Response.Write(".estiloGenerales")
Response.Write("{mso-style-parent:style0;")
Response.Write("font-size:7.0pt;")
Response.Write("font-family:'Calibri', 'Arial Narrow', sans-serif;")
Response.Write("mso-font-charset:0;}")

Response.Write(".estiloTituloMini")
Response.Write("{mso-style-parent:style0;")
Response.Write("font-size:5.0pt;")
Response.Write("font-family:'Calibri', 'Arial Narrow', sans-serif;")
Response.Write("mso-font-charset:0;}")
Response.Write("</STYLE>")

' sept 2011 a como me chinga esto de la orientacion landscape...


' a ver si con esto ... SI, pues con esto SI meta orientacion pero pierde los anchos de columnas
'http://answerleaks.com/how-to-freeze-the-header-row-in-an-excel-spreadsheet-exported-from-asp-net/it/2335166

Response.Write("<!--[if gte mso 9]><xml>" + Environment.NewLine)
Response.Write("<x:ExcelWorkbook>" + Environment.NewLine)
Response.Write("<x:ExcelWorksheets>" + Environment.NewLine)
Response.Write("<x:ExcelWorksheet>" + Environment.NewLine)
Response.Write("<x:Name>Preliminares</x:Name>" + Environment.NewLine)
Response.Write("<x:WorksheetOptions>" + Environment.NewLine)


' cuando quitamos el Print... se fue a horizontal y de cualquier manera emparejo todos los anchos
Response.Write("<x:Print>" + Environment.NewLine)
Response.Write("<x:ValidPrinterInfo/>" + Environment.NewLine) ' ESTA LINEA VOLTEO TODO A VERTICAL!!
'Response.Write("<x:PaperSizeIndex>9</x:PaperSizeIndex>" + Environment.NewLine)
'Response.Write("<x:HorizontalResolution>600</x:HorizontalResolution" + Environment.NewLine)
'Response.Write("<x:VerticalResolution>600</x:VerticalResolution" + Environment.NewLine)
Response.Write("</x:Print>" + Environment.NewLine)

'Response.Write("<x:Selected/>" + Environment.NewLine)
Response.Write("<x:DoNotDisplayGridlines/>" + Environment.NewLine)
'Response.Write("<x:ProtectContents>False</x:ProtectContents>" + Environment.NewLine)
'Response.Write("<x:ProtectObjects>False</x:ProtectObjects>" + Environment.NewLine)
'Response.Write("<x:ProtectScenarios>False</x:ProtectScenarios>" + Environment.NewLine)

Response.Write("</x:WorksheetOptions>" + Environment.NewLine)
Response.Write("</x:ExcelWorksheet>" + Environment.NewLine)
Response.Write("</x:ExcelWorksheets>" + Environment.NewLine)


'Response.Write("<x:WindowHeight>12780</x:WindowHeight>" + Environment.NewLine) ' propiedades de la ventana excel al abrirse
'Response.Write("<x:WindowWidth>19035</x:WindowWidth>" + Environment.NewLine)
Response.Write("<x:WindowTopX>0</x:WindowTopX>" + Environment.NewLine)
Response.Write("<x:WindowTopY>15</x:WindowTopY>" + Environment.NewLine)
Response.Write("<x:ProtectStructure>False</x:ProtectStructure>" + Environment.NewLine)
Response.Write("<x:ProtectWindows>False</x:ProtectWindows>" + Environment.NewLine)
Response.Write("</x:ExcelWorkbook>" + Environment.NewLine)
Response.Write("</xml><![endif]-->" + Environment.NewLine)

Response.Write("</head><body>")

Response.Write("<table>")
' Viaciamos los titulos generales de columnas
Response.Write("<tr>")
'Response.Write("<td width=20 style='width:20pt'></td>") ' lugar
'Response.Write("<td width=20 style='width:20pt'></td>") ' moto
'Response.Write("<td width=20 style='width:8pt'></td>") ' espacio
'Response.Write("<td width=71 style='width:80pt'>PILOTO</td>") ' nombre ' OK ESTO FUNCIONO PARA LOS ANCHOS
'Response.Write("<td width=20 style='width:40pt'></td>") ' marca
'Response.Write("<td width=20 style='width:20pt'></td>") ' estado

'----------------
' Coloca los titulos generales de las velocidades de acuerdo a las ETAPAS que haya
'---------------

For j As Integer = 0 To rsTemporal.FieldCount - 1
' si se definio algun titulo custom para la columna en excel, se vacia aqui

'Response.Write("<td><b><center>" & rsTemporal.GetName(j) & "</center></b></td>")
Response.Write("<td><b><center>" & arrTitulos(j) & "</center></b></td>")
Next


Response.Write("</tr>")

'----------------------
' CICLO PRIMARIO DE LECTTURA DE REGISTROS
'----------------------
While rsTemporal.Read()

Response.Write("<tr>")

For j As Integer = 0 To rsTemporal.FieldCount - 1
Dim valorX As String = ""
valorX = rsTemporal.Item(j).ToString()
'If Not IsDBNull(rsCarreras("test_total")) Then test_total = rsCarreras("test_total")
Response.Write("<td class='estiloGenerales' x:str=""'" & Server.HtmlEncode(valorX) & """>" & Server.HtmlEncode(valorX) & "</td>")
Next

Response.Write("</tr>")
End While 'Next
Response.Write("</table>")
Response.Write("</body></html")

Etiquetas: excel, net, recordset, sql, visual
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:05.