EPPlus: How do I get 2 ExcelPackage.SaveAs?

I am using EPPlus to export to Excel. I need to have 2 SaveAs, 1st SaveAs, which is a save dialog that allows the Open / Save / SaveAs user and my second SaveAs to allow saving the Excel file directly to the specified folder on the server as a backup.

So my problem here is that my second SaveAs is not working (error pop-up during debugging, no files are generated for the second SaveAs).

I ask for advice. Thanks!

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

Below are the codes (my 1st option is SaveAs for the user who must choose 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 following code does not work (my second SaveAs to save the file directly to the server):

 string path = @"C:\Users\testacc\Desktop\Test\" + filename +";"; Stream stream = File.Create(path); package.SaveAs(stream); stream.Close(); byte[] data = File.ReadAllBytes(path); 
+1
source share
2 answers

Why not look at it and achieve another, in the reverse order. First save the generated file to the server, and then transfer the saved file to the client.

  • Create package

     ExcelPackage package = new ExcelPackage(); ..... code for loading data table ..... var filename = @"REPORT_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx"; 
  • Save to server

     string path = @"C:\Users\testacc\Desktop\Test\" + filename +";"; Stream stream = File.Create(path); package.SaveAs(stream); stream.Close(); 
  • Transferring the saved file to the client

     try { response.Clear(); response.ContentType = "application/vnd.xlsx"; response.AddHeader("content-disposition", "attachment; filename=" + filename + ";"); response.TransmitFile(path); response.Flush(); } catch (Exception ex) { // any error handling mechanism } finally { HttpContext.Current.ApplicationInstance.CompleteRequest(); } 
+2
source

Anytime you call .Save , .SaveAs or .GetAsByteArray() (there may be others) in EPPlus, they have the side effect of closing the package. Therefore, you will need to rebuild your package by doing something like package = new ExcelPackage.. and somehow re-reading the file.

Since you already have the packet in memory, why not just copy the bytes and save it twice, avoiding extra trips to IO. Something like that:

 ExcelPackage package = new ExcelPackage(); var workbook = package.Workbook; var hoja = workbook.Worksheets.Add("Sheet1"); //Copy the package in memory byte[] data = package.GetAsByteArray(); //Write to web Response.OutputStream.Write(data, 0, data.Length); //Write to file var filename = @"REPORT_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx"; path = @"C:\temp\" + filename + ";"; stream = File.Create(path); stream.Write(data, 0, data.Length); stream.Close(); 
+1
source

Source: https://habr.com/ru/post/1271711/


All Articles