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()
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()