Generate Excel in .NET with Bytescout Spreadsheet SDK

  • Home
  • /
  • Generate Excel in .NET with Bytescout Spreadsheet SDK

Generate Excel XLS, XLSX, CSV files
in your .NET applications.

Bytescout Spreadsheet SDK is a clean and simple API to generate Excel files in .NET applications.

Almost in every business application we need to have the ability to create Excel files. Using Bytescout Spreadsheet SDK is a simple way of Excel generation.

With the Spreadsheet SDK, you can generate Excel files in C#, ASP.NET, Visual Basic .NET.

Generate Excel – C#:

using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Spreadsheet;
using System.IO;
using System.Diagnostics;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create new Spreadsheet
            Spreadsheet document = new Spreadsheet();

            // Add new worksheet
            Worksheet worksheet = document.Workbook.Worksheets.Add("HelloWorld");

            // Set cell value
            worksheet.Cell(0, 0).Value = "Hello, World!";
            // delete output file if exists already
            if (File.Exists("Output.xls")){
                File.Delete("Output.xls");
            }

            // Save document
            document.SaveAs("Output.xls");

            // Close Spreadsheet
            document.Close();

            // open generated XLS document in default program
            Process.Start("Output.xls");
        }
    }
}

Generate Excel – ASP.NET:

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

Generate Excel – VB.NET:

Imports Bytescout.Spreadsheet
Imports System.IO

Module Module1

    Sub Main()
        Dim document As New Spreadsheet()

        Dim worksheet As Worksheet = document.Workbook.Worksheets.Add("HelloWorld")

        worksheet.Cell(0, 0).Value = "Hello, World!"

        ' remove output file if already exists
        If File.Exists("Output.xls") Then
            File.Delete("Output.xls")
        End If

        ' Save document
        document.SaveAs("Output.xls")

        ' Close Spreadsheet
        document.Close()

        ' open in default spreadsheets viewer/editor
        Process.Start("Output.xls")

    End Sub

End Module

Find all sample source codes for C#, ASP.NET and VB.NET in a free trial of Spreadsheet SDK. Download now!

More about Bytescout Spreadsheet SDK benefits

Tutorials: