How to export dynamically generated html table to Excel Sheet

S

Smitha Bavandla

I have an aspx page("for generating report") in which i have 4 table
tags("<table></table>" for heading of report etc.) and the rows and cells to
them are dynamically created based on the values retrieved from database in
the form od dataset which results 4 tables. For ex:
Dim HeaderRow = New HtmlTableRow
Dim HeaderCell = New HtmlTableCell
HeaderCell.Height = "5"
HeaderCell.Attributes.Add("ALIGN", "center")
HeaderCell.Attributes.Add("CLASS", "ins-popupheader")
HeaderCell.InnerText = ds.Tables(0).Rows(0)("Title") & " -
Content Report"
HeaderRow.Cells.Add(HeaderCell)
tblHeader.Rows.Add(HeaderRow)

Now the problem is.... I have an anchor tag on my report page "Export as
CSV" on click of which I need to export the contents of the page to an Excel
sheet.
I tried with the following code:

Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition",
"attachment;filename=Ebom.xls")

Dim dsExport As DataSet = Session("Contentds")
Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim dgGrid As New DataGrid

Dim i As Integer = 0
For Each table As DataTable In dsExport.Tables
dgGrid.DataSource = dsExport.Tables(i)
dgGrid.DataBind()
dgGrid.RenderControl(hw)
i = i + 1
Next

' Write the HTML back to the browser.
Response.Write(tw.ToString())

' End the response.
Response.End()

It generates an Excel sheet but the format is not clear and I am missing
some contents of my report page. I request if any one can help me in directly
exporting the contents of my report page to Excel.
A quick response is appreciated........
Thanking in anticipation
--smitha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top