Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Jan 24, 2006 12:13 PM|LINK
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.
By chance if the user checks off the option "Always ask before opening this type of file" that is shown in the dialog box, 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
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
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";
System.IO.StringWriter oStringWriter =new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
string strStyle =@"
objStreamWriter = File.AppendText(strFilePath);
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.