Send excel file to user in web

Private Sub ExportFile(ByVal strfilePath As String, ByVal strFilename As String)
Try
Response.Clear()
Response.ContentType = “application/vnd.ms-excel”
strfilePath = Server.MapPath(strfilePath)
If Not System.IO.File.Exists(strfilePath) Then
strfilePath = Server.MapPath(“~//Data//ExportToExcel//NoRecord.xls”)
Response.AddHeader(“content-disposition”, (“attachment;filename=” + (“NoRecord” + “.xls”)))
Else
Response.AddHeader(“content-disposition”, (“attachment;filename=” _
+ (strFilename + “.xls”)))
End If

Dim sourcefile As FileStream = New FileStream(strfilePath, FileMode.Open)
Dim filesize As Long
filesize = sourcefile.Length
Dim getcontent() As Byte = New Byte((CType(filesize, Integer)) – 1) {}
sourcefile.Read(getcontent, 0, CType(sourcefile.Length, Integer))
sourcefile.Close()
Response.BinaryWrite(getcontent)
Catch ex As Exception
ex = ex
Finally
Response.End()
End Try

End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s