It writes the file to the server first and then the user is able to download it and save or open it. The problem is that I have never been able to delete the file on the server after the process is finished. I have tried deleting it in code and, evidently the server just doesn't permit deleting through code. I also tried using a SQL Server job to delete all CSV files on a regular basis, but the server won't permit that either. What I have to do is periodically go in my ftp client and manually delete all the unused CSV files so they don't get too numerous.
ds = GetDataSet(inv)
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
Response.ContentType = "application/vnd.msexcel"
Dim sSheetName As String
Dim sDate As String = Month(Now.ToShortDateString) & Day(Now.ToShortDateString ) & Year(
Now.ToShortDateString)
sSheetName = ReportName & sStore & "_" & sDate & ".csv"
Dim FilePath As String = Server.MapPath("reports/" & sSheetName)
CreateXLSFile(FilePath, s.ToString)
Response.Redirect("reports/" & sSheetName)
End Sub
Private Sub CreateXLSFile(ByVal FilePath As String, ByVal TextToAdd As String)
Try
Dim sw As StreamWriter = New StreamWriter(FilePath)
sw.Write(TextToAdd)
sw.Close()
Catch ex As Exception
End Try
End Sub