Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
When exporting Datagrid to excel you might need the file to open instantly without saving it. You can easily achieve this by using the code below. Just comment out the Response.Cache line and it will work fine.

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

Response.Charset = "";

// Comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

Posted on Friday, August 5, 2005 11:21 AM | Back to top


Comments on this post: Open Datagrid export Excel file instantly without saving it.

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
when I open the file and I want to do "Save As" tell's me that is a webpage file... and I can not make an import.
Left by Luminita on Sep 08, 2005 5:00 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
You need to see that if you are using the correct path to the Excel File.
Left by Azamsharp on Sep 08, 2005 5:20 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
Thanks, very helpful
Left by SD on Sep 08, 2005 7:32 PM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
Data export from datagrid to excel is in wrong format. E.g:suppose the text is 00123 but i only manage to get 123 in excel file, which is wrong data.how to solve it?
Left by YinG on Jun 12, 2006 4:21 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
When i try to export the datas in the datagrid. the complete page is downloaded into the excel sheet. I need only the data's in data grid to be loaded. Can you help me
Left by Samuel on Sep 18, 2006 1:17 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
HI!

I have a requirement where i bind the data to datagrid after post back, and i use to create bounded columns dynamically using C# and after it is displayed i want to export the data to Excel, but the above code is not working in this scenario.

Thanks in advace
Left by Ram PRatap on Sep 18, 2006 2:24 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
Hi.when I open the excel file..I have no means of importing the changes made in the excel sheet..Hoping you would help me out as soon as possible
Left by Kiran on Dec 19, 2006 6:44 AM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
Thanks a lot, it was exactly what I needed, very helpful
Left by Haegeth on Oct 04, 2007 12:29 PM

# re: Open Datagrid export Excel file instantly without saving it.
Requesting Gravatar...
while printing the exported data in excel file via datagrid,some columns are not displayed in printout.
Please guide me.
Thanks
Rohit
Left by Rohit on Jan 10, 2008 6:10 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net