如何在导出excel后重新加载aspx网页。 [英] How to reload the aspx web page after export excel.

查看:93
本文介绍了如何在导出excel后重新加载aspx网页。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net中将数据导出为ex​​cel时,它运行正常。下载对话框正在显示,但页面没有重新加载。



导出到excel函数,在Response.End()函数之后,没有语句正常工作。



如何解决这个问题..



我的尝试:



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:

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

推荐答案

请求只能有一个响应,因此您的响应可以是是文件下载或javascript刷新页面,它不能两者兼而有之。您能够做到这一点的唯一真正方法是在不同的窗口中启动下载,因此在您的下载页面上,您可以在新窗口中打开下载请求,方法是使用javascript提交包含下载页面的表单。 action和target =_ blank然后相同的js将刷新当前页面。另请注意,您的代码不会将任何内容导出到Excel,它只是告诉客户端使用Excel来解析html表。



预先设置您的下一个问题;下载文件后无法进行刷新,因为无法知道文件是否已下载。
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.


这篇关于如何在导出excel后重新加载aspx网页。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆