Question

I have the following code to save for Excel export:

'Export to Excel
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=excelExport.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"

        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)

        Dim grdView = New GridView

        grdView.AllowPaging = False

        'LOAD DATA
        Dim Sql As String = "SELECT * " & _
             "FROM bobSettings AS bobSettings " & _
              "INNER JOIN bobData AS bobData " & _
             "ON bobSettings.linkTobobData = bobData.linkTobobSettings " & _
             "WHERE linkTobobSettings = '1110450005' "

        Dim checkFormName As New SqlCommand(Sql, myCONN)

        myCONN.Open()

        Dim objCmd As SqlCommand

        objCmd = New SqlCommand(Sql, myCONN)

        Dim da As New SqlDataAdapter(objCmd)
        Dim ds As New DataSet()
        da.Fill(ds)

        grdView.DataSource = ds
        grdView.DataBind()
        myCONN.Close()
        'END LOADING DATA

        grdView.DataBind()
        grdView.HeaderRow.Style.Add("background-color", "#FFFFFF")
        grdView.HeaderRow.Cells(0).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(1).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(2).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(3).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(4).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(5).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(6).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(7).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(8).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(9).Style.Add("background-color", "#DFB")
        grdView.HeaderRow.Cells(10).Style.Add("background-color", "#DFB")

        For i As Integer = 0 To grdView.Rows.Count - 1
            Dim row As GridViewRow = grdView.Rows(i)

            row.BackColor = System.Drawing.Color.White
            row.Attributes.Add("class", "textmode")

            If i Mod 2 <> 0 Then
                row.Cells(0).Style.Add("background-color", "#C2D69B")
                row.Cells(1).Style.Add("background-color", "#C2D69B")
                row.Cells(2).Style.Add("background-color", "#C2D69B")
                row.Cells(3).Style.Add("background-color", "#C2D69B")
                row.Cells(4).Style.Add("background-color", "#C2D69B")
                row.Cells(5).Style.Add("background-color", "#C2D69B")
                row.Cells(6).Style.Add("background-color", "#C2D69B")
                row.Cells(7).Style.Add("background-color", "#C2D69B")
                row.Cells(8).Style.Add("background-color", "#C2D69B")
                row.Cells(9).Style.Add("background-color", "#C2D69B")
                row.Cells(10).Style.Add("background-color", "#C2D69B")
            End If
        Next
        grdView.RenderControl(hw)

        Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

        Response.Write(style)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
        ds = Nothing
        da = Nothing

And it executes without problems or errors BUT i can not seem to find where it places the file or do it ever pop up a box asking me to save it, etc.

What am i missing?

Was it helpful?

Solution

You write

grdView.DataSource = ds

then

ds = Nothing

and then

grdView.DataBind()

EDIT:

To avoid "Thread was being aborted" you may put Response.End() in a Try Catch block to handle ThreadAbort exception.

Try
    Response.End()
Catch e As ThreadAbortException
End Try
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top