Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using Visual Studio with C# in ASPX pages. WebApplication.
I am using OfficeOpenXml for writing the Excel file.

I have written the following code to write the excel file.

Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=StateList_" + DateTime.Now.ToString("yyyyMMyyHHmmss") + ".xlsx");
var myMS = new MemoryStream();
excel.SaveAs(myMS);
myMS.WriteTo(Response.OutputStream);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();


Also I have added VerifyRenderingInServerForm event.

This code is working on one page and it is not working on another. Can any one help what i might have done wrong.

What I have tried:

I have also tried another option as follows:
string mydocpath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo outputDir = new DirectoryInfo(mydocpath);

FileInfo excelFile = new FileInfo(outputDir.FullName + @"\StateList_" + DateTime.Now.ToString("yyyyMMyyHHmmss") + ".xlsx");

excel.SaveAs(excelFile);

This code is working and creating the file on the server side. I need the file saving on the client side irrespective of OS.
Posted
Updated 14-Nov-19 2:35am
v2
Comments
F-ES Sitecore 14-Nov-19 9:21am    
You can't save files to the client file system, your code runs on the server so when you do things like GetFolderPath you are getting the path on the server, and when you SaveAs you are saving on the server. If you want to send files to the client you have to use the code you're using in the first code sample. To preempt your next questions, no you can't force them to save, you can't dictate what folder it saves in and you can't save without prompting. You wouldn't want a website saving files on your machine so you can't do it to others either.

As for not working on a different page, there is nothing in your code that is page specific so the problem will lie elsewhere.
Richard Deeming 14-Nov-19 9:24am    
What does not working mean? Are you getting an error?

Your second code block obviously won't work; the code is running on the server, and has no access to the user's file system.
LingaMurthy 16-Nov-19 2:39am    
I am not getting any error. on one page it is giving prompt for saving the file. on another page the prompt is not coming for saving. compared both aspx and code behind in c#. nothing found, if you give me the mail I can send the files to you so that any help can be provided... thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900