Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 676 Bytes

Deliver-an-Excel-file-in-ASP.NET.md

File metadata and controls

23 lines (19 loc) · 676 Bytes

ASP.NET Web Forms

// Create the workbook
XLWorkbook workbook = new XLWorkbook();
workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World");

// Prepare the response
HttpResponse httpResponse = Response;
httpResponse.Clear();
httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
httpResponse.AddHeader("content-disposition", "attachment;filename=\"HelloWorld.xlsx\"");

// Flush the workbook to the Response.OutputStream
using (MemoryStream memoryStream = new MemoryStream())
{
  workbook.SaveAs(memoryStream);
  memoryStream.WriteTo(httpResponse.OutputStream);
  memoryStream.Close();
}

httpResponse.End();