将动态asp表导出到Excel

时间:2014-01-16 08:48:26

标签: html asp.net export-to-excel

我在代码后面创建了一个动态asp表,并有一个导出函数将表导出到Excel, 但输出Excel是空白的,没有任何数据。

如果导出函数是静态html表,则它可以正常工作。

是否有任何方法可以导出动态asp表?

<asp:Table id="tbl_data" runat="server" Width="95%" BackColor="White" BorderColor="Black" 
BorderWidth="1" ForeColor="Black" GridLines="Both" BorderStyle="Solid">
</asp:Table>

代码背后:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then           
       Dim trr1 As New TableRow
        Dim tdr1_1 As New TableCell
        Dim tdr1_2 As New TableCell
        Dim tdr1_3 As New TableCell
        Dim i As Integer = 0

        'Table Header

        '2nd row
        Dim tr As New TableRow
        Dim td1 As New TableCell
        td1.Text = "Staff</br>No."

        Dim td2 As New TableCell
        td2.Text = "Display name (Know As)"

        Dim td3 As New TableCell
        td3.Text = "Current</br>Project/Office"

        Dim td4 As New TableCell
        td4.Text = "Current</br>Department"

        Dim td5 As New TableCell
        td5.Text = "Current</br>Title"

        tr.Cells.Add(td1)
        tr.Cells.Add(td2)
        tr.Cells.Add(td3)
        tr.Cells.Add(td4)
        tr.Cells.Add(td5)

        'another table cells
        'xxxxxxxxxxxxxxxxxxxxxxx

        tbl_data.Rows.AddAt(tbl_data.Rows.Count, tr)

    end if
end sub

Protected Sub RadToolBar1_ButtonClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs) Handles RadToolBar1.ButtonClick
    If e.Item.Value = "Export" Then
        Dim sw As New StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(sw)
        Dim frm As HtmlForm = New HtmlForm()

        Page.Response.AddHeader("content-disposition", "attachment;filename=PDR_Submission_Status_Report.xls")
        Page.Response.ContentType = "application/vnd.ms-excel"
        Page.Response.Charset = ""
        Page.EnableViewState = False
        frm.Attributes("runat") = "server"
        Controls.Add(frm)
        frm.Controls.Add(tbl_data)
        frm.RenderControl(hw)
        Response.Write(sw.ToString())
        Response.End()

    End If
End Sub 

2 个答案:

答案 0 :(得分:1)

点击按钮(If Not Page.IsPostBack Then)后,您的表格不会生成,因此您无法导出数据。

在If Postback块之外做表生成,一切都会好的:)

答案 1 :(得分:0)

代码下方将为您提供帮助。如果您在任何服务器变量中设置动态生成表并使用它,那么它将起作用。

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Dim trr1 As New TableRow
        Dim tdr1_1 As New TableCell
        Dim tdr1_2 As New TableCell
        Dim tdr1_3 As New TableCell
        Dim i As Integer = 0
        Dim tr As New TableRow
        Dim td1 As New TableCell
        td1.Text = "Staff</br>No."
        Dim td2 As New TableCell
        td2.Text = "Display name (Know As)"
        Dim td3 As New TableCell
        td3.Text = "Current</br>Project/Office"
        Dim td4 As New TableCell
        td4.Text = "Current</br>Department"
        Dim td5 As New TableCell
        td5.Text = "Current</br>Title"
        tr.Cells.Add(td1)
        tr.Cells.Add(td2)
        tr.Cells.Add(td3)
        tr.Cells.Add(td4)
        tr.Cells.Add(td5)
        tbl_data.Rows.AddAt(tbl_data.Rows.Count, tr)
        Session("TempData") = tbl_data
    End If
  End Sub
  Protected Sub btn_Click(sender As Object, e As System.EventArgs) Handles btn.Click
    Dim sw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(sw)
    Dim frm As HtmlForm = New HtmlForm()

    Page.Response.AddHeader("content-disposition", "attachment;filename=PDR_Submission_Status_Report.xls")
    Page.Response.ContentType = "application/vnd.ms-excel"
    Page.Response.Charset = ""
    Page.EnableViewState = False
    frm.Attributes("runat") = "server"
    Controls.Add(frm)
    frm.Controls.Add(Session("TempData"))
    frm.RenderControl(hw)
    Response.Write(sw.ToString())
    Response.End()
  End Sub