windows:descargarcomoexcel
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| windows:descargarcomoexcel [2010/08/26 12:15] – creado rlunaro | windows:descargarcomoexcel [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 11: | Line 11: | ||
| He cogido un sniffer de red y he visto qué es lo que contesta el servidor web cuando se descarga un archivo cvs. Es, ni más ni menos, que el contenido de este zip: | He cogido un sniffer de red y he visto qué es lo que contesta el servidor web cuando se descarga un archivo cvs. Es, ni más ni menos, que el contenido de este zip: | ||
| - | {{: | + | {{: |
| Ahora, si reproducimos **exactamente** el formato, hará lo mismo, digo yo, no??? | Ahora, si reproducimos **exactamente** el formato, hará lo mismo, digo yo, no??? | ||
| + | |||
| + | ===== Segundo paso: hacer el desarrollo ==== | ||
| + | |||
| + | < | ||
| + | <%@ Language=VBScript %> | ||
| + | <% | ||
| + | |||
| + | ' OJO!!!! ESTO NO FUNCIONA EN MODO DEPURACION | ||
| + | ' OJO!!!! ESTO NO FUNCIONA EN MODO DEPURACION | ||
| + | ' OJO!!!! ESTO NO FUNCIONA EN MODO DEPURACION | ||
| + | ' OJO!!!! ESTO NO FUNCIONA EN MODO DEPURACION | ||
| + | |||
| + | Response.Expires = 0 | ||
| + | server.scriptTimeOut = 3600 ' una hora | ||
| + | |||
| + | dim oTest | ||
| + | dim rs | ||
| + | |||
| + | set oTest = server.CreateObject(" | ||
| + | |||
| + | set rs = oTest.hacerunaconsultaquedevuelvaunrecordset( "" | ||
| + | |||
| + | |||
| + | dim oUtil | ||
| + | dim sTable | ||
| + | |||
| + | set oUtil = server.CreateObject(" | ||
| + | |||
| + | sTable = oUtil.exportQuery( rs, 0 ) | ||
| + | |||
| + | sContentType = " | ||
| + | sFileName = " | ||
| + | |||
| + | Response.AddHeader " | ||
| + | Response.ContentType = sContentType | ||
| + | Response.AddHeader " | ||
| + | Response.AddHeader " | ||
| + | Response.AddHeader " | ||
| + | ' cabecera para que el dialogo que aparezca en el navegador del cliente es | ||
| + | ' " | ||
| + | ' navegador. | ||
| + | Response.AddHeader " | ||
| + | |||
| + | Response.write sTable | ||
| + | |||
| + | Response.End | ||
| + | |||
| + | </ | ||
| + | |||
| + | Y el código fuente de la funcion visual basic que convierte un recordset a tabla: | ||
| + | |||
| + | < | ||
| + | ' nFormat = 1 - to HTML < | ||
| + | ' nFormat = 0 - to CVS data; | ||
| + | Public Function exportQuery(ByVal rsIn As Object, _ | ||
| + | | ||
| + | |||
| + | Dim rsParam As adodb.Recordset | ||
| + | Dim oUtil As Object | ||
| + | Dim sql As String | ||
| + | Dim functionName As String | ||
| + | |||
| + | ' pon aquí el nombre de la funcion, para registro de errores | ||
| + | functionName = " | ||
| + | |||
| + | On Error GoTo catch | ||
| + | |||
| + | Set oUtil = doNew(" | ||
| + | |||
| + | Dim sTableTemplate As String | ||
| + | Dim sRowTemplate As String | ||
| + | Dim sColumnTemplate As String | ||
| + | Dim nColumn As Long | ||
| + | Dim sTr As String | ||
| + | Dim sTd As String | ||
| + | Dim sTable As String | ||
| + | Dim sFilteredSemicolon As String | ||
| + | Dim sSeparator As String | ||
| + | |||
| + | Select Case nFormat | ||
| + | Case 0 | ||
| + | ' para sacar un contenido cvs | ||
| + | sTableTemplate = " | ||
| + | sRowTemplate = " | ||
| + | sColumnTemplate = """ | ||
| + | sSeparator = ";" | ||
| + | Case 1 | ||
| + | ' para sacar una tabla html | ||
| + | sTableTemplate = "< | ||
| + | sTableTemplate = sTableTemplate & " | ||
| + | sTableTemplate = sTableTemplate & "</ | ||
| + | sRowTemplate = "< | ||
| + | sRowTemplate = sRowTemplate & " | ||
| + | sRowTemplate = sRowTemplate & "</ | ||
| + | sColumnTemplate = "< | ||
| + | sSeparator = "" | ||
| + | Case Else | ||
| + | ' para sacar un contenido cvs | ||
| + | sTableTemplate = " | ||
| + | sRowTemplate = " | ||
| + | sColumnTemplate = """ | ||
| + | sSeparator = ";" | ||
| + | End Select | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | sTable = "" | ||
| + | If Not rs_empty(rsIn) Then | ||
| + | |||
| + | sTr = "" | ||
| + | |||
| + | ' for every record | ||
| + | nColumn = 0 | ||
| + | sTd = "" | ||
| + | Do While nColumn < rsIn.Fields.Count | ||
| + | | ||
| + | If nFormat = 1 Then | ||
| + | sTd = concat(sTd, Replace(sColumnTemplate, | ||
| + | Else | ||
| + | sFilteredSemicolon = Replace(rsIn.Fields(nColumn).Name, | ||
| + | sTd = concat(sTd, Replace(sColumnTemplate, | ||
| + | End If | ||
| + | nColumn = nColumn + 1 | ||
| + | Loop | ||
| + | sTr = sTr & Replace(sRowTemplate, | ||
| + | | ||
| + | rsIn.MoveFirst | ||
| + | Do While Not rsIn.EOF | ||
| + | | ||
| + | ' for every record | ||
| + | nColumn = 0 | ||
| + | sTd = "" | ||
| + | Do While nColumn < rsIn.Fields.Count | ||
| + | | ||
| + | If nFormat = 1 Then | ||
| + | sTd = concat(sTd, Replace(sColumnTemplate, | ||
| + | Else | ||
| + | sFilteredSemicolon = Replace(rs_field(rsIn, | ||
| + | sTd = concat(sTd, Replace(sColumnTemplate, | ||
| + | End If ' nFormat = 1 | ||
| + | | ||
| + | nColumn = nColumn + 1 | ||
| + | Loop | ||
| + | sTr = concat(sTr, Replace(sRowTemplate, | ||
| + | | ||
| + | | ||
| + | rsIn.MoveNext | ||
| + | Loop | ||
| + | sTable = concat(sTable, | ||
| + | |||
| + | End If ' not rs_empty(rsIn) | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | final: | ||
| + | |||
| + | doDel oUtil | ||
| + | |||
| + | exportQuery = sTable | ||
| + | |||
| + | Exit Function | ||
| + | |||
| + | catch: | ||
| + | | ||
| + | Dim txtError As String | ||
| + | |||
| + | txtError = functionName & ": " & Err.Description | ||
| + | |||
| + | oUtil.EscribirLog txtError | ||
| + | |||
| + | |||
| + | ' | ||
| + | exportQuery = "" | ||
| + | |||
| + | ' | ||
| + | doDel oUtil | ||
| + | | ||
| + | Err.Raise Err.Number, App.EXEName, | ||
| + | |||
| + | End Function | ||
| + | |||
| + | ' performs the operation cBase = cBase & cIn | ||
| + | ' adding a separator (cSep) if needed | ||
| + | Private Function concat(ByVal cBase As String, ByVal cIn As String, Optional ByVal cSep As String = "" | ||
| + | |||
| + | If Len(cBase) = 0 Then | ||
| + | concat = cIn | ||
| + | Else | ||
| + | concat = cBase & cSep & cIn | ||
| + | End If ' len(cBase) | ||
| + | |||
| + | End Function | ||
| + | |||
| + | </ | ||
windows/descargarcomoexcel.1282824957.txt.gz · Last modified: 2022/12/02 21:02 (external edit)
