Generating XLS documents from ASP.NET code using Bytescout.XLS for .NET

This sample source code is designed to show how to generate Excel (XLS) document from ASP.NET using Bytescout.XLS library and then write produced document into browser using Response object (implementing on-the-fly output)

Download example source code: bytescoutxls_asp_net.zip (2 KB)

Excel (XLS) document generated from ASP.NET application using Bytescout.XLS

To run Bytescout.XLS on a ASP.NET project just do the following:

1) Run Visual Studio 2005
2) Create new “Web-Site” project using Project Wizard
3) Click “Website” menu and then “Add Reference” command in this menu
4) “Add Reference” dialog will appear. Select “Bytescout.XLS” and click OK to add a reference to Bytescout.XLS component
5) Double-click “Default.aspx.cs” in Solution Explorer and then copy and paste the following code into it:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bytescout.XLS;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create new document
XLSDocument document = new XLSDocument();

// Add “HelloWorld” worksheet
Worksheet worksheet = document.WorkBook.Worksheets.Add(“HelloWorld”);

// Set cell B2 value “HelloWorld”
worksheet.Cell(0, 0).Value = “HelloWorld”;

// clear http output
Response.Clear();
// set the content type to PDF
Response.ContentType = “application/xls”;
// add content type header
Response.AddHeader(“Content-Type”, “application/xls”);
// set the content disposition
Response.AddHeader(“Content-Disposition”, “inline;filename=HelloWorld.xls”);
// write the buffer with pdf file to the output
document.SaveToStream(Response.OutputStream);

Response.End();
}
}

Then run ASP.NET web-site using F5 button

Download example source code: bytescoutxls_asp_net.zip (2 KB)