all groups > asp.net datagrid control > november 2003 >
You're in the

asp.net datagrid control

group:

ASP.NET DataGrid and Excel


ASP.NET DataGrid and Excel Milan Todorovic
11/24/2003 9:31:18 AM
asp.net datagrid control:
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

Re: ASP.NET DataGrid and Excel Alvin Bruney
11/24/2003 9:40:46 AM
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

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
[quoted text, click to view]

Re: ASP.NET DataGrid and Excel Richard K Bethell
11/24/2003 10:55:23 AM
[quoted text, click to view]

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.

Re: ASP.NET DataGrid and Excel Milan Todorovic
11/24/2003 11:39:53 AM
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


[quoted text, click to view]

Re: ASP.NET DataGrid and Excel Jack Mullins
11/26/2003 7:11:24 AM
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
Re: ASP.NET DataGrid and Excel Nebojsa Gogic
12/18/2003 11:22:32 PM
I am using Office Web Components:

SpreadsheetClass xlsheet = new SpreadsheetClass();

for(int l=0;l<DataGrid1.Columns.Count;l++)
{
xlsheet.ActiveSheet.Cells[1,l+1]=DataGrid1.Columns[l].HeaderText;
}

for(int k=0;k<ds1.Tables[0].Rows.Count;k++)
{
for(int j=0;j<ds1.Tables[0].Columns.Count;j++)
{

xlsheet.ActiveSheet.Cells[iRow,j+1]=ds1.Tables[0].Rows[k].ItemArray.GetValue
(j).ToString();
}
iRow++;
}

OWC.Range range;
range=xlsheet.get_Range("A1","Z1");
range.AutoFitColumns();
xlsheet.ActiveSheet.Cells.AutoFitColumns();
string strFile ="report" + System.DateTime.Now.Ticks.ToString() +".xls";
xlsheet.ActiveSheet.Export(strCurrentDir+strFile,OWC.SheetExportActionEnum.s
sExportActionNone);
......
Nebojsa Gogic
[quoted text, click to view]

AddThis Social Bookmark Button