Skip to content

Deliver an Excel file in ASP.NET

Francois Botha edited this page Jan 25, 2017 · 1 revision

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();

FAQ

Examples

Real world scenarios

Time Savers

Performance and Memory

Misc

Inserting Data/Tables

Styles

Ranges

Rows

Columns

Page Setup (Print Options)

AutoFilters

Comments

Dev docs

Clone this wiki locally