0

我有以下代码要保存以用于 Excel 导出:

'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

它执行没有问题或错误但我似乎无法找到它放置文件的位置或是否弹出一个框要求我保存它等。

我错过了什么?

4

1 回答 1

0

你写

grdView.DataSource = ds

然后

ds = Nothing

进而

grdView.DataBind()

编辑:

为了避免“线程被中止”,您可以放入Response.End()一个 Try Catch 块来处理 ThreadAbort 异常。

Try
    Response.End()
Catch e As ThreadAbortException
End Try
于 2012-11-16T20:53:55.217 回答