"";
// If you want the option to open the Excel file without saving then
// comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
aobjPage.Response.ContentType = "application/vnd.xls";
System.IO.
StringWriter stringWrite =
new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
igvInstance.RenderControl(htmlWrite);
aobjPage.Response.Write(stringWrite.ToString());
aobjPage.Response.End();
}
}
///Note special add a refrence for axcel 5.0 or later dll.
public override
void VerifyRenderingInServerForm(Control control)
"";
// If you want the option to open the Excel file without saving then
// comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
aobjPage.Response.ContentType = "application/vnd.xls";
System.IO.
StringWriter stringWrite =
new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
igvInstance.RenderControl(htmlWrite);
aobjPage.Response.Write(stringWrite.ToString());
aobjPage.Response.End();
}
}
and add refference of microsoft excell 5.0 or later
and use also this code
public
override void VerifyRenderingInServerForm(Control control)
"application/vnd.xls"
Dim stringWrite
As System.IO.StringWriter =
New System.IO.StringWriter()Dim htmlWrite
As System.Web.UI.HtmlTextWriter =
New HtmlTextWriter(stringWrite)
grdLicenses.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
Mukesh the line in bold is cause for your issue..once u comment that line ..excel will open without any error,,,,
1) You can add an "onclick" attribute to the button as follows:
btnExportToExcel.Attributes.Add(
"onclick",
"window.open('./ReportInExcel.aspx?Parameter1=Value1&Parameter2=Value2')")
2) Create the the same Gridview on the ReportInExcel.aspx page and add the following in the next line to the <%@Page Language=........ %> line on this page.
Ok, now one of my PCs is cursed with Excel saying that it could not find the temp .xls file in the ...\temporary internet files\Content.IE5 folder and to check the spelling blah blah blah. I have looked at lots of threads with people having the same problem
and as far as I can tell nobody seems to really have an answer.
Most of the threads have source code similar to this one with some minor changes to the order of the lines of code. Some people have suggested to not use the line of code "Response.Cache.SetCacheability(HttpCacheability.NoCache);" while others seem to
be OK with it. In some threads people say the problem goes away after changing this or that but I have tried all the combinations without success on my problem PC.
Now to add my own two riduculous workarounds which my users just love to hear me suggest, I have found the following:
1) Tell users to previously open Excel on the client desktop and then run the ASP.NET code in IE and the error will be avoided. This of course isn't a real workaround since you can't expect users to do this extra step and the spreadsheet after loading does
not pop up in front of ASP.NET program which causes confusion.
2) dump Internet Explorer and use Firefox instead
To me, this problem seems like a intermittent timing issue where Excel is sometimes taking too long to open or the temp .xls file is not ready to open yet to to an issue in IE. I have made sure that my problem PC is fully updated with Microsoft patches
but that did not make any difference. Once in a while, my problem PC does not have the error on the first attempt after starting the program but then seems to always fail on the second attempt. I have run the problem PC using ASP.NET code using IIS on both
a production server and on a local IIS instance on the problem PC without any difference.
Surely, someone has a better solution to this problem. Please let me know if anyone has any fresh ideas.
One of the reasons could be the size of the Excel file. There is a limit on the Excel file size if you create by exporting from ASP.NET. If you are loading too many data it may be exceeding that size limit. Try with smaller set of data just to test.
The error where Excel cannot open the temp .xls file in the ...\temporary internet files\content.ie5 folder happens on small files with only a few rows as well as files with a couple of thousand rows. I also experimented with changing IE to use c:\temp\temporary
internet files instead but this didn't change anything.
It also happens on my problem PC pointing to the production server where other PCs pointing to exactly the same ASP.NET application and identical Excel files and have never had this error show up.
The problem must be related to something unique to this PC.
nareshguree2...
Star
11118 Points
1997 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Sep 07, 2007 06:55 AM|LINK
public void ExportGridView(GridView igvInstance, System.Web.UI.Page aobjPage, string astrFileName){
aobjPage.Response.Clear();
aobjPage.Response.AddHeader("content-disposition", "attachment;filename="+ astrFileName +".xls");aobjPage.Response.Charset =
""; // If you want the option to open the Excel file without saving then // comment out the line below //Response.Cache.SetCacheability(HttpCacheability.NoCache); aobjPage.Response.ContentType = "application/vnd.xls";System.IO.
StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);igvInstance.RenderControl(htmlWrite);
aobjPage.Response.Write(stringWrite.ToString());
aobjPage.Response.End();
}
}
///Note special add a refrence for axcel 5.0 or later dll.
public override void VerifyRenderingInServerForm(Control control){
}
asp.net 2.0 export to excel
nareshguree2...
Star
11118 Points
1997 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Sep 07, 2007 07:09 AM|LINK
public void ExportGridView(GridView igvInstance, System.Web.UI.Page aobjPage, string astrFileName){
aobjPage.Response.Clear();
aobjPage.Response.AddHeader("content-disposition", "attachment;filename="+ astrFileName +".xls");aobjPage.Response.Charset =
""; // If you want the option to open the Excel file without saving then // comment out the line below //Response.Cache.SetCacheability(HttpCacheability.NoCache); aobjPage.Response.ContentType = "application/vnd.xls";System.IO.
StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);igvInstance.RenderControl(htmlWrite);
aobjPage.Response.Write(stringWrite.ToString());
aobjPage.Response.End();
}
}
and add refference of microsoft excell 5.0 or later
and use also this code
public override void VerifyRenderingInServerForm(Control control){
}
cheer up
regard
excel
bhanup
Member
4 Points
2 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Sep 13, 2007 06:03 AM|LINK
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=COMPCODE.xls")Response.Charset =
"" 'Response.Cache.SetCacheability(HttpCacheability.NoCache)Response.ContentType =
"application/vnd.xls" Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)grdLicenses.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
Mukesh the line in bold is cause for your issue..once u comment that line ..excel will open without any error,,,,
kirit_raja
Member
61 Points
47 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Sep 13, 2007 03:06 PM|LINK
I just add the following line of code after the <%@ Page Language=........ %> line
<!- <%response.ContentType="application/vnd.ms-excel"%>
And it does the job. Hope this helps.
pareshpmahad...
Member
2 Points
1 Post
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 05, 2007 11:00 AM|LINK
Hi
Can pls help me out for code to export Entire Gridview only to new Window on button Click event...its an Web application.
With Regards
Paresh
kirit_raja
Member
61 Points
47 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 05, 2007 02:36 PM|LINK
Hi Paresh,
Here are the steps:
1) You can add an "onclick" attribute to the button as follows:
btnExportToExcel.Attributes.Add(
"onclick", "window.open('./ReportInExcel.aspx?Parameter1=Value1&Parameter2=Value2')") 2) Create the the same Gridview on the ReportInExcel.aspx page and add the following in the next line to the <%@Page Language=........ %> line on this page.<!-
<%response.ContentType="application/vnd.ms-excel"%>Hope this helps.
davidgreenbe
Member
53 Points
40 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 28, 2007 02:42 PM|LINK
Ok, now one of my PCs is cursed with Excel saying that it could not find the temp .xls file in the ...\temporary internet files\Content.IE5 folder and to check the spelling blah blah blah. I have looked at lots of threads with people having the same problem and as far as I can tell nobody seems to really have an answer.
Most of the threads have source code similar to this one with some minor changes to the order of the lines of code. Some people have suggested to not use the line of code "Response.Cache.SetCacheability(HttpCacheability.NoCache);" while others seem to be OK with it. In some threads people say the problem goes away after changing this or that but I have tried all the combinations without success on my problem PC.
Now to add my own two riduculous workarounds which my users just love to hear me suggest, I have found the following:
1) Tell users to previously open Excel on the client desktop and then run the ASP.NET code in IE and the error will be avoided. This of course isn't a real workaround since you can't expect users to do this extra step and the spreadsheet after loading does not pop up in front of ASP.NET program which causes confusion.
2) dump Internet Explorer and use Firefox instead
To me, this problem seems like a intermittent timing issue where Excel is sometimes taking too long to open or the temp .xls file is not ready to open yet to to an issue in IE. I have made sure that my problem PC is fully updated with Microsoft patches but that did not make any difference. Once in a while, my problem PC does not have the error on the first attempt after starting the program but then seems to always fail on the second attempt. I have run the problem PC using ASP.NET code using IIS on both a production server and on a local IIS instance on the problem PC without any difference.
Surely, someone has a better solution to this problem. Please let me know if anyone has any fresh ideas.
Thanks,
David
kirit_raja
Member
61 Points
47 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 28, 2007 07:39 PM|LINK
One of the reasons could be the size of the Excel file. There is a limit on the Excel file size if you create by exporting from ASP.NET. If you are loading too many data it may be exceeding that size limit. Try with smaller set of data just to test.
davidgreenbe
Member
53 Points
40 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 28, 2007 08:47 PM|LINK
The error where Excel cannot open the temp .xls file in the ...\temporary internet files\content.ie5 folder happens on small files with only a few rows as well as files with a couple of thousand rows. I also experimented with changing IE to use c:\temp\temporary internet files instead but this didn't change anything.
It also happens on my problem PC pointing to the production server where other PCs pointing to exactly the same ASP.NET application and identical Excel files and have never had this error show up.
The problem must be related to something unique to this PC.
David
kirit_raja
Member
61 Points
47 Posts
Re: How To: Make "Export to Excel" always open excel in a separate Window
Nov 29, 2007 03:58 PM|LINK
Did you check permissions on the temporary folder?