Your table is not generated after your button click (If Not Page.IsPostBack Then
), so therefore you have no data to export.
Do the table-generation outside the If Postback block, and all will be fine :)
Pregunta
I created a dynamic asp table in code behind and had an export function to export the table to Excel, but the output Excel is blank without any data.
The export function works properly if it is a static html table.
Is there any method to export a dynamic 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>
Code behind:
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
Solución
Your table is not generated after your button click (If Not Page.IsPostBack Then
), so therefore you have no data to export.
Do the table-generation outside the If Postback block, and all will be fine :)
Otros consejos
Below Code will help you.If you set dynamic generate table in any server variable and use it then it will work.
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