HttpContext.Current.Response.End()跳过执行

时间:2014-06-02 08:33:47

标签: javascript asp.net excel gridview webmethod

我使用gridview通过JavaScriptwebmethod导出到Excel工作表。由于gridview中的所有行都包含label,anchor,etc。这样的模板控件,它们是在运行时通过JavaScript而非设计创建的,因此我使用将所有行按数组传递给web方法。要导出我创建了一个表并映射JavaScript函数返回的数组。但问题是在调试时,httpContext.Current.Response.End会跳过执行,而且我也无法导出网格。我不知道为什么会这样。下面是我的网络方法代码

<WebMethod(EnableSession:=True, transactionOption:=EnterpriseServices.TransactionOption.RequiresNew)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Xml, usehttpget:=True)> _
Public Shared Sub XlExport(ByVal fileName As String, ByVal row As Object()())
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim table As Table = New Table
    table.GridLines = GridLines.Both
    For i As Integer = 0 To row.Length - 1
        Dim dr As New TableRow
        For j As Integer = 0 To row(i).Length - 1
            Dim dc As New TableCell
            Dim lt As New LiteralControl
            lt.Text = row(i)(j)
            dc.Controls.Add(lt)
            dr.Cells.Add(dc)
        Next
        table.Rows.Add(dr)
    Next
    table.RenderControl(htw)
    HttpContext.Current.Response.Write(sw.ToString)
    HttpContext.Current.Response.End()
End Sub

1 个答案:

答案 0 :(得分:0)

我自己尝试过。我将所有行存储在隐藏字段中。并在服务器端我将它们转换为数组然后表。现在我使用上面的代码作为普通函数而不是webmethod

sub download_click()
    Dim str_arr As Object()
    Dim str_arr1 As Object()
    str_arr = hdn_gvcheck.Value.Split(",")
    str_arr1 = hdn_headerdiv.Value.Split(",")
    Dim len As Integer = (str_arr.Length - 2) / 36
    Dim twoDarr As Object()() = New Object(len - 1)() {}
    len = (str_arr1.Length - 2) / 36
    Dim headArr As Object()() = New Object(len - 1)() {}
    Dim ind As Integer = 0
    Dim start As Integer = 1
    Dim fnsh As Integer = 36
    headArr(ind) = New Object(35) {}
l1:
    twoDarr(ind) = New Object(35) {}

    Dim col As Integer = 0
    For i As Integer = start To fnsh
        twoDarr(ind)(col) = str_arr(i)
        If ind = 0 Then
            headArr(ind)(col) = str_arr1(i)
        End If

        col += 1
    Next
    If ind < 7 Then
        ind += 1
        start = fnsh + 1
        fnsh = start + 35
        GoTo l1
    End If
    doExport("position.xls", twoDarr, headArr, gvcheck)
End Sub

Sub doExport(ByVal fileName As String, ByVal row As Object()(), ByVal head As Object()(), ByVal gv As GridView)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.Buffer = True
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
    HttpContext.Current.Response.Charset = ""
    Dim table As Table = New Table
    table.GridLines = gv.GridLines 'GridLines.Both
    'For i As Integer = 0 To head.Length - 1
    Dim HDR As New TableRow
    For j As Integer = 0 To head(0).Length - 1
        Dim dc As New TableCell
        dc.Style("font-weight") = "bold"
        Dim lt As New LiteralControl(head(0)(j))
        'lt.Text = row(i)(j)
        dc.Controls.Add(lt)
        HDR.Cells.Add(dc)
    Next
    table.Rows.Add(HDR)
    'Next
    For i As Integer = 0 To row.Length - 1
        Dim dr As New TableRow
        For j As Integer = 0 To row(i).Length - 1
            Dim dc As New TableCell
            Dim lt As New LiteralControl(row(i)(j))
            'lt.Text = row(i)(j)
            dc.Controls.Add(lt)
            dr.Cells.Add(dc)
        Next
        table.Rows.Add(dr)
    Next
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    table.RenderControl(htw)
    HttpContext.Current.Response.Write(sw.ToString) 'sw.ToString
    HttpContext.Current.Response.End()
End Sub