ASP.NET DataGrid and Excel

M

Milan Todorovic

I desperately need help.

I have an ASP.NET (in VB.NET) app that displays the data within the datagrid
object. I need to allow for exporting of that data to the Excel spreadsheet.
I have tried using Excel (referenced) object, but for some reason I can't
get it to work. I get all sorts of COM errors.
I'm looking for the simplest solution, since I do not care about any Excel
formatting or functionalities.

Thanks

Milan Todorovic
 
A

Alvin Bruney

The easiest solution here would be dump the datagrid out as a csv or html
file to disk. Then, load the file into the spreadsheet using the csvdata or
htmdata property of the spreadsheet object.

Regards
 
R

Richard K Bethell

Milan Todorovic said:
I desperately need help.

I have an ASP.NET (in VB.NET) app that displays the data within the datagrid
object. I need to allow for exporting of that data to the Excel spreadsheet.
I have tried using Excel (referenced) object, but for some reason I can't
get it to work. I get all sorts of COM errors.
I'm looking for the simplest solution, since I do not care about any Excel
formatting or functionalities.

Here's what we did. Offer a pop-up window from your page to return the data
as Excel. In the asp.net page for the popup, render the same datatable to a
datagrid, and return the page with the Response.ContentType MIME type set to
'application/x-msexcel'

R.
 
M

Milan Todorovic

Richard,
That was actually the advice I was hoping for.
Now, I have a question or two about this approach. Apparently, if you create
xls file by using Response.ContentType = "application.x-msexcel" the
spreadsheet fails to open, due to the presence of the datagrid object (i.e.
if the datagrid is not there it works fine). However, I could use the
datatable (datagrid source) and build html table that I can stream into
response object. The question I have is this: is it possible (if yes, how)
to accomplish this without creating the new page (which would create a clean
Response object). That is, is it possible to use the Response object of the
existing page?
I'll keep on researching this idea.
Thanks for the pointer
Milan
 
J

Jack Mullins

Here is some code I found and have tried with success
objAdapter.SelectCommand = New OleDbCommand(strSQL, objConn

objAdapter.Fill(objDataset
' Create a new view
Dim oView As New DataView(objDataset.Tables(0)
' Set up the data grid and bind the data
DataGrid1.DataSource = oVie
DataGrid1.DataBind(

' Verify if the page is to be displayed in Excel
If Request.QueryString("bExcel") = "1" The
' Set the content type to Excel
Response.ContentType = "application/vnd.ms-excel
' Remove the charset from the Content-Type header
Response.Charset = "
' Turn off the view state
Me.EnableViewState = Fals

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

' Get the HTML for the control
DataGrid1.RenderControl(hw
' Write the HTML back to the browser
Response.Write(tw.ToString()
' End the response
Response.End(
End I

End Su

Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChange

End Su
End Clas
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top