locked
Export to excel problem RRS feed

  • Question

  • User507221241 posted

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click

            Response.ContentType = "application/vnd.ms-excel"

            Dim tw As New System.IO.StringWriter

            Dim hw As New System.Web.UI.HtmlTextWriter(tw)

     

            Dim attachment As String = "attachment; filename=EventLogs.xls"

            Response.Clear()

            Response.Buffer = True

            Response.ClearContent()

            Response.AddHeader("content-disposition", attachment)

            Response.Charset = ""

            Me.EnableViewState = False

            GridView1.DataSource = SessionEventDetails.GetSessionEventDetails(txtFromDate.Text, txtToDate.Text, txtSearchUsername.Text, txtSearchIPAddress.Text)

            GridView1.DataBind()

            GridView1.AllowPaging = False

            GridView1.AllowSorting = False

            Dim frm As HtmlForm = New HtmlForm()

            GridView1.Parent.Controls.Add(frm)

            frm.Attributes("runat") = "server"

            frm.Controls.Add(GridView1)

            frm.RenderControl(hw)

     

            ' //GridView1.RenderControl(htw);

     

            Response.Write(tw.ToString())

            Response.End()

          

        End Sub

    -------------------------------------------------------------------------------------------------------------------------

    This is my code for Export to excel.Above code is working properly but when the user tries to save it the Default File Type is Web
    Page(*.htm; *.html)....I want to restrict it as a .xls file type. How to restrict the save as file type to the Excel file type?? Plzzzzzzzzzz help... :(

    Monday, March 15, 2010 8:30 AM

All replies

  • User1011739529 posted

    Hi,

    Add the following code after Resoponse.AddHeader line and try again:

    Response.ContentType = "application/ms-excel"


     

    Monday, March 15, 2010 9:45 AM