Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when export data to excel in asp.net , it is working fine. The download dialog box is showing, but the page is not reloading.

in export to excel function, after the Response.End() function, no statement is working.

how to solve this issue..

What I have tried:

VB
Private Sub ExporttoExcel(ByVal table As DataTable)




    Try
        Dim excel_name As String = "Call-Log-Monitor"
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.ClearHeaders()
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
        ' HttpContext.Current.Response.ContentType = "text/csv"
        HttpContext.Current.Response.Write("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">")
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + excel_name + "(" + System.DateTime.Now + ").xls")

        HttpContext.Current.Response.Charset = "utf-8"
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250")
        'sets font
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>")
        HttpContext.Current.Response.Write("<BR><BR><BR>")
        'sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " + "borderColor='#000000' cellSpacing='0' cellPadding='0' " + "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>")
        'am getting my grid's column headers
        Dim columnscount As Integer = GridView1.Columns.Count

        For j As Integer = 0 To columnscount - 1
            'write in new column
            HttpContext.Current.Response.Write("<Td>")
            'Get column headers  and make it as bold in excel columns
            HttpContext.Current.Response.Write("")
            HttpContext.Current.Response.Write(GridView1.Columns(j).HeaderText.ToString())
            HttpContext.Current.Response.Write("")
            HttpContext.Current.Response.Write("</Td>")
        Next
        HttpContext.Current.Response.Write("</TR>")
        For Each row As DataRow In table.Rows
            'write in new row
            HttpContext.Current.Response.Write("<TR>")
            For i As Integer = 0 To table.Columns.Count - 1
                HttpContext.Current.Response.Write("<Td>")
                HttpContext.Current.Response.Write(row(i).ToString())
                HttpContext.Current.Response.Write("</Td>")
            Next

            HttpContext.Current.Response.Write("</TR>")
        Next



        HttpContext.Current.Response.Write("</Table>")
        HttpContext.Current.Response.Write("</font>")

        HttpContext.Current.Response.Flush()


        'Dim location As String = HttpContext.Current.Request.Url.AbsoluteUri


        HttpContext.Current.Response.[End]()


        'ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "", "alert('" + location + "');", True)


        ' Page.ClientScript.RegisterStartupScript(Me.GetType(), "Redirect", String.Format("this.document.href = '{0}'", location), True)


    Catch ex As Exception
    Finally

    End Try

End Sub
Posted
Updated 31-Jan-17 23:19pm

1 solution

A request can only have one response so your response can either be the file download or javascript to refresh the page, it can't be both. The only real way you'll be able to do this is to instigate the download in a different window, so on your download page you can open the download request in a new window by using javascript to submit a form with your download page as the action and target="_blank" then the same js will refresh the current page. Note also that your code is not exporting anything to Excel, it is simply telling the client to use Excel to parse an html table.

To pre-empt your next questions; you can't do the refresh after the file has been downloaded as there is no way of knowing if the file has been downloaded.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900