Monday, January 23, 2006

ASP.NET : Make "Export to Excel" always open excel in a separate Window

Export to Excel in ASP.NET is a very common feature, which I'm sure everyone who has worked in ASP.NET would have had the chance to implement.

Whenever we choose the Export to Excel option from our Application, a dialog box pops us with the option to Open or to Save, as shown below:

By chance if the user checks off the option "Always ask before opening this type of file", from next time the user will not be able to see the dialog box. Instead, the excel file opens up in the same window.

To set back this option, the following steps can be followed:

1. Go to Windows Explorer.
2. On the Tools menu, click Folder Options, and then click on the File Types tab.
3. From the Registered file types list box, select the XLS extension, and then click Advanced.
4. In the Edit File Type dialog box, set the Confirm open after download to selected.
5. Make sure the Browse in same window option is not selected, and then click OK.

The above steps will make sure that we get the dialog box as shown above. However, since this is an option set at the client computer, these steps cannot be mandated to be followed in every computer that browses the application.

So, from the code level, we must make sure that the excel file is opened in a separate window. One possible option for this is to Save the file to the web server, and then open the file in a separate window.

The code for this is given below:

private void ExportToExcel(DataGrid dgExport)
string strFileName = String.Empty, strFilePath= String.Empty;
strFilePath = Server.MapPath(@"../Excel/") + "ExcelFileName" + ".xls";
if (File.Exists(strFilePath))
System.IO.StringWriter oStringWriter =new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
StreamWriter objStreamWriter;
string strStyle =@"
objStreamWriter = File.AppendText(strFilePath);
string strScript = "<script language=JavaScript>'../Excel/" + "ExcelFileName" +

Page.RegisterStartupScript("clientScript", strScript);
//Handle Exception

In the above method, the file is saved to the Web Server inside the folder "Excel". Of course, this folder must have write permissions for the user. But it will definitely ensure that the excel file is opened in a new window in the client computer.



Sainduboy said...

Reaaly helpful...

harsha said...

Trying to Export Resume to ms word in but css file missing....
Hi all,
Trying to Export Resume to ms word in, but css file missing when opening resume doc after saving it.

im writing as...

aspx file...

using resume privew control here

title Resume Preview /title

style type="text/css" title="currentStyle"

@import url(style/Master_Import.css);
@import url(style/JobDetails.css);
@import url(style/ResumePreview.css);
@import url(style/ResPreview.css);


private void Resumedownload_Click(object sender, System.EventArgs e)
Response.ContentType = "application/vnd.xls";
ResumePreview.CandidateId = Convert.ToInt32(Request.QueryString["CandId"]);

getting window to open or save, when im opening its ok, but whn im saving it & reopening word file
css files are missing


Hyderabadi's said...

Hi Lakshmi,

Thanks for the code.
I used the code in my application.
When I retrive the .xls page, it is retriving the old page which is stored in the temporary internet files, even though the new .xls page is beging stored in the folder.
If i go to tools/ internet options/temperory internet files
and delete then, then it is retrevign the new version of the page.
Please let me know how to deal with this problem.
Its really urgent.
Reply to me at

appanparige said...

After data is saved in file which in Server path, I am unable to delete the file which is in server path. Code is not executed after window is displayed for save.


G said...

This works abs fine but my requirement is to show the Data in the instance of the excel instead of IE. I am using IE 6.0 and seems nothing working for it.

Any help will be appreciated

stan said...

You are the man! This helped me out big time :)

Sumit said...

cooollllllll...Thanks a lottt dude...u saved me big time....

Alex said...

If you have this situation,then I recommend to use this application-Microsoft Excel the file error: there is a possibility that data is lost.,which in my view one of the best in this sphere,it has free status,also software is used to process important information, such as graphics, diagrams, commercial and statistical data,recover *.xlsx, *.xlsm, *.xltm, *.xltx or *.xlam formats,tool will help you if you have to repair Excel file error message: Excel error the file is not in a recognizable format,allows choosing a file, that shown an error like: Excel error message this file is not in a recognizable format, then, you should fix Excel file for errors and proceed with its analysis,attempts to recover your document with this message: Excel error and this file is not in a recognizable format and shows a preview window.

Sharmin said...

Any idea with the missing css file issue? I could not find it any where.