Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi members,

I am using EP Plus for my export to excel function, for my export function, I will need to have 2 saveas, 1st saveas which is a save dialogue to allow user open/save/saveas and my 2nd saveas to allow the excel file to be saved directly into the specified folder in the server as a backup copy.

Thus my issue here is that my 2nd saveas does not work (no error popup during debug, no files generated either for 2nd saveas).
Please advice. thanks!

C#
ExcelPackage package = new ExcelPackage();
......
.......
.....
codes for loading data table
.....
....         
.....
            var filename = @"REPORT_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx";

The below codes works (my 1st saveas for user to choose to open/save/saveas):

            Response.Clear();
            package.SaveAs(Response.OutputStream);
            Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xlsx";
            Response.End();

The below code does not work(my 2nd saveas to save file directly into server): 

            string path = @"C:\Users\testacc\Desktop\Test\" + filename +";";
            Stream stream = File.Create(path);
            package.SaveAs(stream);
            stream.Close();
            byte[] data = File.ReadAllBytes(path);
Posted
Updated 19-Oct-15 23:50pm
v2
Comments
ZurdoDev 20-Oct-15 8:17am    
What in the world does "does not work mean?" If you called your mechanic on the phone and said, "my car does not work" do you think they could fix it over the phone? We need a lot more details, like an error message.

Looking at your code, you probably are missing the proper permissions and I'll bet if you would look at the error you'll see that is what it is telling you.

1 solution

Calling the SaveAs method multiple times will have unpredictable results. Instead, save the package on the server, and then use the Response.TransmitFile method to send the file to the client:
C#
string filename = string.Format(@"REPORT_{0:dd-MM-yyyy_hh-mm-ss}.xlsx", datetime);
var file = new FileInfo(Path.Combine(@"C:\Users\testacc\Desktop\Test\", filename));
package.SaveAs(file);

Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
Response.TransmitFile(file.FullName);
Application.CompleteRequest();
 
Share this answer
 

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