This is one of those "it-works-on-my-machine, but-it-doesn't-work-when-I-upload-it" kind of things. I think everyone has gone through this several times in their career.
In tests it works and downloads data in the form of a csv file that writes straight to the OutputStream. I've uploaded all the code and when I test it... you guessed it -- nothing happens!
It doesn't even return an error. Anybody know what's happening??
Private Function CreateSpreadsheet(ByVal inv As String, ByVal sta As String, ByVal rpt As String, ByVal ReportName As String) As String
Dim i, c, r As Int32, ds As DataSet, colLabel(1) As String, msg As String = ""
Dim s As New StringBuilder, temp As String
ds = GetDataSet(inv, sta, rpt)
GetHeader(colLabel, ds)
Dim rowcount As Int32 = ds.Tables(0).Rows.Count()
Dim colcount As Short = ds.Tables(0).Columns.Count
For i = 0 To colcount - 1
c = i + 1
s.Append(colLabel(i) & ",")
Next
s.Append(vbCr)
For r = 0 To rowcount - 1
For c = 1 To colcount
If Not IsDBNull(ds.Tables(0).Rows(r)(c - 1)) Then
temp = Replace(Trim(ds.Tables(0).Rows(r)(c - 1)), ",", "")
temp = Replace(temp, vbCr, " ")
temp = Replace(temp, vbLf, " ") & ","
Else
temp = ","
End If
s.Append(temp)
Next
s.Append(vbCr)
Next
Dim sSheetName As String
Dim sDate As String = Month(Now.ToShortDateString) & Day(Now.ToShortDateString) & Year(Now.ToShortDateString)
sSheetName = ReportName & "_" & sDate & ".csv"
msg = CreateXLSFile(sSheetName, s.ToString)
Return msg
End Function
Private Function CreateXLSFile(ByVal fileName As String, ByVal TextToAdd As String) As String
Dim context As System.Web.HttpContext, msg As String
context = System.Web.HttpContext.Current
msg = ""
context.Response.Clear()
context.Response.ContentType = "application/x-msexcel"
Dim b() As Byte
b = StrToByteArray(TextToAdd)
Dim lngLength As Long = b.LongLength
Try
context.Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
context.Response.OutputStream.Write(b, 0, lngLength)
context.Response.End()
Catch ex As Exception
msg = ex.Message
End Try
Return msg
End Function
In tests it works and downloads data in the form of a csv file that writes straight to the OutputStream. I've uploaded all the code and when I test it... you guessed it -- nothing happens!
It doesn't even return an error. Anybody know what's happening??
Private Function CreateSpreadsheet(ByVal inv As String, ByVal sta As String, ByVal rpt As String, ByVal ReportName As String) As String
Dim i, c, r As Int32, ds As DataSet, colLabel(1) As String, msg As String = ""
Dim s As New StringBuilder, temp As String
ds = GetDataSet(inv, sta, rpt)
GetHeader(colLabel, ds)
Dim rowcount As Int32 = ds.Tables(0).Rows.Count()
Dim colcount As Short = ds.Tables(0).Columns.Count
For i = 0 To colcount - 1
c = i + 1
s.Append(colLabel(i) & ",")
Next
s.Append(vbCr)
For r = 0 To rowcount - 1
For c = 1 To colcount
If Not IsDBNull(ds.Tables(0).Rows(r)(c - 1)) Then
temp = Replace(Trim(ds.Tables(0).Rows(r)(c - 1)), ",", "")
temp = Replace(temp, vbCr, " ")
temp = Replace(temp, vbLf, " ") & ","
Else
temp = ","
End If
s.Append(temp)
Next
s.Append(vbCr)
Next
Dim sSheetName As String
Dim sDate As String = Month(Now.ToShortDateString) & Day(Now.ToShortDateString) & Year(Now.ToShortDateString)
sSheetName = ReportName & "_" & sDate & ".csv"
msg = CreateXLSFile(sSheetName, s.ToString)
Return msg
End Function
Private Function CreateXLSFile(ByVal fileName As String, ByVal TextToAdd As String) As String
Dim context As System.Web.HttpContext, msg As String
context = System.Web.HttpContext.Current
msg = ""
context.Response.Clear()
context.Response.ContentType = "application/x-msexcel"
Dim b() As Byte
b = StrToByteArray(TextToAdd)
Dim lngLength As Long = b.LongLength
Try
context.Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
context.Response.OutputStream.Write(b, 0, lngLength)
context.Response.End()
Catch ex As Exception
msg = ex.Message
End Try
Return msg
End Function