Link Search Menu Expand Document

Getting Started in ASP.NET

The following example demonstrates writing a simple spreadsheet document using Spreadsheet class.

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

  • Run Visual Studio 2005
  • Create new "Web-Site" project using Project Wizard
  • Click "Website" menu and then "Add Reference" command in this menu
  • "Add Reference" dialog will appear. Select "Bytescout.Spreadsheet" and click OK to add a reference to Bytescout.Spreadsheet component
  • Copy and paste the following code into the "Default.aspx.cs" file (see below)

using System;
using System.Data;
using System.Configuration;
using System.IO;
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.Spreadsheet;

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

        // 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 XLS (to open with Excel)
        Response.ContentType = "application/xls";
        // add content type header
        Response.AddHeader("Content-Type", "application/xls");
        // set the content disposition
        Response.AddHeader("Content-Disposition", "attachment;filename=HelloWorld.xls"); // change "attachment" to "inline" if you want to appear Excel editor right inside the browser instead of File Save dialog
        // write the buffer with xls spreadsheet file to the output
        document.SaveToStream(Response.OutputStream);

        Response.End();
    }
}