html - 将动态 asp table 导出到 excel

  显示原文与译文双语对照的内容

我在代码 behind 中创建了一个动态 asp table,并具有导出函数以将 table 导出到 excel 。 但输出excel为空,没有任何数据。

如果导出函数是 static html table,则导出功能正常工作。

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


<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 

时间: 作者:

你的table 不是在按钮单击之后生成的( If Not Page.IsPostBack Then ),因此,你没有导出的数据。

如果在 postback block 之外生成表,那么一切都将正常:)

作者:

below 代码将帮助 you.If 在任何服务器变量中设置动态生成 table,然后使用它。


 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

作者:
...