Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this, you can get the <code>Request</code> and <code>Response</code> directly off <code>HttpContext.Current</code>:</p> <pre><code>public static void SendExcelFile(string downloadFileName, List&lt;List&lt;string&gt;&gt; data, string worksheetTitle) { var context = HttpContext.Current; string tempFolder = context.Request.PhysicalApplicationPath + "temp"; string tempFileName = tempFolder + "tempFileName.xlsx" if (!Directory.Exists(tempFolder)) Directory.CreateDirectory(tempFolder); // Generate file using ExcelPackage GenerateExcelDoc(tempFileName, data, worksheetTitle); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName); context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; context.Response.AddHeader("Content-Length", new FileInfo(tempFileName).Length.ToString()); context.Response.TransmitFile(tempFileName); File.Delete(tempFileName); } </code></pre> <p>Another alternative is a base class for your pages that contains this method, that may be a much easier route. You pages don't have to inherit from System.Web.UI.Page, they can inherit from something else, like this:</p> <pre><code>public class BasePage : System.Web.UI.Page { public void SendExcelFile(string downloadFileName, List&lt;List&lt;string&gt;&gt; data, string worksheetTitle) { string tempFolder =Request.PhysicalApplicationPath + "temp"; string tempFileName = tempFolder + "tempFileName.xlsx" if (!Directory.Exists(tempFolder)) Directory.CreateDirectory(tempFolder); // Generate file using ExcelPackage GenerateExcelDoc(tempFileName, data, worksheetTitle); Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Length", new FileInfo(tempFileName).Length.ToString()); Response.TransmitFile(tempFileName); File.Delete(tempFileName); } } </code></pre> <p>Then in your page the class looks like:</p> <pre><code>public partial class MyPage : BasePage { //Stuff! } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload