NativeExcel for .NET Developer's Reference

ASP.NET Hello World

Example

A simple example for ASP.NET

[C#]
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 NativeExcel;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void HelloWorldButton_Click(object sender, EventArgs e)
    {
         //Create workbook
         IWorkbook book = NativeExcel.Factory.CreateWorkbook();
         IWorksheet sheet = book.Worksheets.Add();

         sheet.Cells["B2"].Value = "Hello World!!!";
         sheet.Cells["B2"].Font.Size = 20;
         sheet.Cells["B2"].ColumnWidth = 50;

         //Stream workbook  
         Response.Clear(); 
         Response.ContentType = "application/vnd.ms-excel"; 
         Response.AddHeader("Content-Type",        "application/vnd.ms-excel");
         Response.AddHeader("Content-Disposition", "attachment;filename=book.xls"); 

         book.SaveAs(Response.OutputStream);

         Response.End();

    }
}
[Visual Basic]
Imports NativeExcel

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub HelloWorldButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles HelloWorldButton.Click
        'Create workbook
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
        Dim sheet As IWorksheet = book.Worksheets.Add()

        sheet.Cells("B2").Value = "Hello World!!!"
        sheet.Cells("B2").Font.Size = 20
        sheet.Cells("B2").ColumnWidth = 50

        'Stream workbook  
        Response.Clear()
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Type", "application/vnd.ms-excel")
        Response.AddHeader("Content-Disposition", "attachment;filename=book.xls")

        book.SaveAs(Response.OutputStream)

        Response.End()
    End Sub
End Class