Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: How do I send data to excel and then refresh aspx page?

$
0
0
I just resolved it. Not sure if this is the best way, but it works.

First ... I created a new page that will save the data to the excel in the page load.

Second ... I save my data to a session variable ... Session("dataTableToExcel") = datatable

Then I call a subroutine that will open a new page in a new window ... OpenNewWindow("\DisplayExcel.aspx")
 Protected Sub OpenNewWindow(ByVal url As String)
        Dim sw As StringWriter = New StringWriter
        sw.WriteLine("<script language=""javascript"">")
        sw.WriteLine("var newWindow = window.open(""{0}"",""SendToExcel""", url)
        sw.Write(",""height=700,width=900,resizable=yes,scrollbars=yes,toolbar=no,menubar=no"");")
        sw.WriteLine("newWindow.focus();")
        sw.WriteLine("</script>")
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "Open New Window", sw.ToString())
    End Sub
The new page looks like this ....

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim dt As DataTable = Session("dataTableToExcel")

            Dim wb As New ClosedXML.Excel.XLWorkbook()
            wb.Worksheets.Add(dt, dt.TableName.ToString)
            Response.Clear()
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            Response.AddHeader("content-disposition", "attachment;filename=DelegateRecordsNotImported.xlsx")

            Using memoryStream As New MemoryStream()
                wb.SaveAs(memoryStream)
                Response.AddHeader("Content-Length", memoryStream.Length.ToString())

                memoryStream.WriteTo(Response.OutputStream)
                memoryStream.Close()

            End Using
        Catch ex As Exception
            Throw
        End Try
    End Sub

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>