
22/10/2014, 01:02
|
| | Fecha de Ingreso: noviembre-2002 Ubicación: DF
Mensajes: 1.056
Antigüedad: 22 años, 2 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") |