Exporting datagrid to Excel Pivot Table

G

Guest

Does anyone have an example of how to export a datagrid to an Excel pivot
table? The code below exports the grid contents to Excel using the Response
object. I'd like to expand it to show the data in a pivot table. Avoiding a
3rd party control is preferable.
Thanks,
Tim

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""

Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

'First write the dgResults grid column names to the datatable
For i = 0 To (Session("dvGrid").Table.Columns.Count - 1)
sOutput = Session("dtResults").Columns(i).columnname() 'get the
column name
dtCounts.Columns.Add(New DataColumn(sOutput, GetType(String)))
Next

'Now write the grid contents to the datatable
For i2 = 0 To Session("dvGrid").Table.Rows.Count - 1
drRow1 = dtCounts.NewRow
For i = 0 To (Session("dvGrid").Table.Columns.Count - 1)
drRow1(i) = Session("dvGrid").Table.Rows(i2).item(i)
Next 'get next cell (column value) in grid line
dtCounts.Rows.Add(drRow1)
Next

With dgCounts
.DataSource = New DataView(dtCounts)
.DataBind()
End With

' Get the HTML for the control.
dgCounts.RenderControl(hw)

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

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top