Wednesday, 08 February 2012 03:08

Create Excel File in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

This section is designed to provide developers a simple solution on how to create Excel file in C#, VB.NET via this .NET Excel component Spire.XLS for .NET.

Spire.XLS for .NET allows you to create Excel file on the formats of Excel (.xls) 97-2003 and Excel (.xlsx) 2007 and Excel(.xlsx)2010 without installing MS Excel or any third party library. Apart from creating Excel file, this Excel api also enables you to read, edit and manipulate Excel files with high efficiency in .NET applications.

Below is the sample code for you to find how you can create an Excel file:

[C#]
using System.Drawing;
using Spire.Xls;

namespace Create_Excel_File
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a new Workboook object
            Workbook workbook = new Workbook();
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Write string values in a cell
            sheet.Range["A3"].Text = "This Excel Document is Created by Spire.XLS for .NET";
            //Save workbook to disk
            workbook.SaveToFile("Sample.xls");
            try
            {
                System.Diagnostics.Process.Start(workbook.FileName);
            }
            catch { }
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Xls

Namespace Create_Excel_File
	Class Program
		Private Shared Sub Main(args As String())
			'Initialize a new Workboook object
			Dim workbook As New Workbook()
			'Get the first worksheet
			Dim sheet As Worksheet = workbook.Worksheets(0)
			'Write string values in a cell
			sheet.Range("A3").Text = "This Excel Document is Created by Spire.XLS for .NET"
			'Save workbook to disk
			workbook.SaveToFile("Sample.xls")
			Try
			System.Diagnostics.Process.Start(workbook.FileName)
			Catch
			End Try
		End Sub
	End Class
End Namespace
          

The Created Excel File

After executing above code, The Excel file is created and some string values are also added in the first Worksheet”Sheet1”. Please see the screenshot below:

Create Excel File

Spire.XLS for .NET is an Excel library which provides developers to perform a wide range of tasks with fast speed, high efficiency and reliability.

More about this .NET Excel Library

Additional Info

  • tutorial_title: Create Excel File
Last modified on Tuesday, 07 January 2014 09:56