我在後面的代碼中創建了一個動態asp表,並且具有將表導出到Excel的輸出函數, 但輸出Excel在沒有任何數據的情況下爲空。將動態asp表導出到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
謝謝...可以導出數據,如果把表生成在外部Postback .. –