Click here to Skip to main content
15,919,423 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Crystal report Pin
Sherin Iranimose22-May-07 20:51
Sherin Iranimose22-May-07 20:51 
GeneralRe: Crystal report Pin
kumarjammula22-May-07 23:34
kumarjammula22-May-07 23:34 
GeneralRe: Crystal report Pin
Sherin Iranimose22-May-07 23:43
Sherin Iranimose22-May-07 23:43 
QuestionData Text Formatting asp.net 2.0 [modified] Pin
Jay_se22-May-07 19:07
Jay_se22-May-07 19:07 
AnswerRe: Data Text Formatting asp.net 2.0 Pin
Sherin Iranimose22-May-07 19:26
Sherin Iranimose22-May-07 19:26 
QuestionData Text Formatting asp.net 2.0 Pin
Jay_se22-May-07 21:06
Jay_se22-May-07 21:06 
QuestionHow to send grid view data into a excel file? Pin
here2learn22-May-07 19:00
here2learn22-May-07 19:00 
AnswerRe: How to send grid view data into a excel file? Pin
Jagadeesh Jupalli22-May-07 19:39
Jagadeesh Jupalli22-May-07 19:39 
This code is collected from "http://forums.asp.net/t/956392.aspx"



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 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)
{
try
{
string strFileName = String.Empty, strFilePath= String.Empty;
strFilePath = Server.MapPath(@"../Excel/") + "ExcelFileName" + ".xls";
if (File.Exists(strFilePath))
{
File.Delete(strFilePath);
}
System.IO.StringWriter oStringWriter =new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
StreamWriter objStreamWriter;
string strStyle =@" .text { mso-number-format:\@; }
";
objStreamWriter = File.AppendText(strFilePath);
dgExport.RenderControl(oHtmlTextWriter);
objStreamWriter.WriteLine(strStyle);
objStreamWriter.WriteLine(oStringWriter.ToString());
objStreamWriter.Close();
string strScript = "window.open('../Excel/" + "ExcelFileName" +
".xls','dn','width=1,height=1,toolbar=no,top=300,left=400,right=1,

scrollbars=no,locaton=1,resizable=1');";
if(!Page.IsStartupScriptRegistered("clientScript"))
{
Page.RegisterStartupScript("clientScript", strScript);
}
}
catch(Exception)
{
//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.

**** I think it helps U ****
AnswerRe: How to send grid view data into a excel file? Pin
varshavmane22-May-07 19:47
varshavmane22-May-07 19:47 
QuestionApplication data caching on a web farm Pin
kaban15622-May-07 18:58
kaban15622-May-07 18:58 
AnswerRe: Application data caching on a web farm Pin
KarasukoJ23-May-07 5:28
KarasukoJ23-May-07 5:28 
QuestionProcess in Asp.net C# Pin
AnhTin22-May-07 18:54
AnhTin22-May-07 18:54 
AnswerRe: Process in Asp.net C# Pin
kubben23-May-07 7:29
kubben23-May-07 7:29 
AnswerRe: Process in Asp.net C# Pin
danimar ribeiro18-Dec-09 5:02
danimar ribeiro18-Dec-09 5:02 
QuestionUsing AJAX in OPERA Pin
248912822-May-07 18:50
248912822-May-07 18:50 
AnswerRe: Using AJAX in OPERA Pin
Christian Graus22-May-07 20:07
protectorChristian Graus22-May-07 20:07 
AnswerRe: Using AJAX in OPERA Pin
szukuro22-May-07 22:30
szukuro22-May-07 22:30 
QuestionOpening attachments in textbox Pin
Rajiya22-May-07 18:17
Rajiya22-May-07 18:17 
AnswerRe: Opening attachments in textbox Pin
N a v a n e e t h22-May-07 20:17
N a v a n e e t h22-May-07 20:17 
GeneralRe: Opening attachments in textbox Pin
Rajiya22-May-07 20:51
Rajiya22-May-07 20:51 
QuestionAdd some more fields for future based..., Pin
Member 387988122-May-07 18:08
Member 387988122-May-07 18:08 
AnswerRe: Add some more fields for future based..., Pin
Sherin Iranimose22-May-07 19:09
Sherin Iranimose22-May-07 19:09 
QuestionHow to time splash screens? Pin
jazz morgan22-May-07 18:03
jazz morgan22-May-07 18:03 
AnswerRe: How to time splash screens? Pin
Christian Graus22-May-07 20:08
protectorChristian Graus22-May-07 20:08 
Questioninsert & play flash file Pin
shufun22-May-07 16:40
shufun22-May-07 16:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.