We help IT Professionals succeed at work.

open excel in asp.net /vb

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I just want to open / download excel sheet using vb.net
it works but it shows blank excel. The excel file actually have data inside.
Can you help how to fix it?

 Dim ExportFileName = "C:\Reports\activemember.xls"
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", ExportFileName))
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        HttpContext.Current.Response.End()

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Normally it looks like this:

Dim ExportFileName = "C:\Reports\activemember.xls"
 Dim fs As FileStream = New FileStream(ExportFileName, FileMode.Open, FileAccess.Read)

        Dim br As BinaryReader = New BinaryReader(fs)

        Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))

            Dim Context = System.Web.HttpContext.Current

            Context.Response.Clear()
            Context.Response.ClearContent()
            Context.Response.ClearHeaders()
            Context.Response.AppendHeader("content-disposition", "attachment;filename=" + System.IO.Path.GetFileName(ExportFileName))
            Context.Response.ContentEncoding = System.Text.Encoding.UTF8
            Context.Response.ContentType = "application/ms-excel"
            Context.Response.AppendHeader("content-length", New FileInfo(ExportFileName).Length)
            Context.Response.BinaryWrite(bytes)
            Context.Response.Flush()
            Context.Response.SuppressContent = True
            Context.ApplicationInstance.CompleteRequest()

Open in new window