I'm writing the contents of my datagrid to an Excel spreadsheet using
the following code:
Sub WriteToExcel(Se nder as Object, E as EventArgs)
'Export the contents of the current datagrid to Excel
Response.Clear( )
Response.Conten tType = "applicatio n/vnd.ms-excel"
Response.AddHea der("content-disposition", "attachment;fil ename=Data.xls" )
Response.Charse t = ""
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Dim stringWrite As StringWriter = New StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter( stringWrite)
dgrExportGrid.R enderControl(ht mlWrite)
Response.Write( stringWrite.ToS tring())
Response.End()
End Sub
This works great, but when I enabled paging on my datagrid, only the
displayed page of data is exported, not the whole dataset. My workaround
is to place a second, non-paged datagrid on my page, populate it with
the same data as my main datagrid, and set its visible property to
false. But my sub now exports a blank Excel spreadsheet. I guess if a
control is invisible, it's invisible to a stringwriter too...?
Can anyone solve this problem or suggest another approach? As always,
any help is much appreciated.
the following code:
Sub WriteToExcel(Se nder as Object, E as EventArgs)
'Export the contents of the current datagrid to Excel
Response.Clear( )
Response.Conten tType = "applicatio n/vnd.ms-excel"
Response.AddHea der("content-disposition", "attachment;fil ename=Data.xls" )
Response.Charse t = ""
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Dim stringWrite As StringWriter = New StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter( stringWrite)
dgrExportGrid.R enderControl(ht mlWrite)
Response.Write( stringWrite.ToS tring())
Response.End()
End Sub
This works great, but when I enabled paging on my datagrid, only the
displayed page of data is exported, not the whole dataset. My workaround
is to place a second, non-paged datagrid on my page, populate it with
the same data as my main datagrid, and set its visible property to
false. But my sub now exports a blank Excel spreadsheet. I guess if a
control is invisible, it's invisible to a stringwriter too...?
Can anyone solve this problem or suggest another approach? As always,
any help is much appreciated.