Add Worksheet in Excel Workbook in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to add worksheet in Excel workbook via Spire.XLS.

using System.Drawing;
using System.Data;
using System.Data.OleDb;

using Spire.Xls;

namespace AddSheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\parts.xls");

            //add one worksheet to workbook
            workbook.CreateEmptySheet("NewSheet");
            
           //set the new sheet's context
            Worksheet sheet = workbook.Worksheets[1];
            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\..\\..\\Data\\database.accdb;Persist Security Info=False;";
            string sqlCountry="select * from vendors";
            DataTable vendor = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(connString))
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlCountry, conn);
                adapter.Fill(vendor);
            }
            sheet.InsertDataTable(vendor, true, 1, 1);

            //set the style of the new sheet
            sheet.AllocatedRange.AutoFitColumns();
            sheet.Range[1, 1, 1, vendor.Columns.Count].Style.Color = Color.DeepSkyBlue;
            sheet.Range[1, 1, 1, vendor.Columns.Count].Style.Font.IsBold = true;
            sheet.Range[1, 1, 1, vendor.Columns.Count].HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Range[2, 1, vendor.Rows.Count + 1, vendor.Columns.Count].Style.Color = Color.SkyBlue;
            sheet.AllocatedRange.Style.Borders.LineStyle = LineStyleType.Thin;
            sheet.AllocatedRange.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.AllocatedRange.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
                 
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports System.Drawing
Imports System.Data
Imports System.Data.OleDb

Imports Spire.Xls

Namespace AddSheet
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\parts.xls")

			'add one worksheet to workbook
			workbook.CreateEmptySheet("NewSheet")

			'set the new sheet's context
			Dim sheet As Worksheet = workbook.Worksheets(1)
			Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\..\..\Data\database.accdb;Persist Security Info=False;"
			Dim sqlCountry As String = "select * from vendors"
			Dim vendor As New DataTable()
			Using conn As New OleDbConnection(connString)
				Dim adapter As New OleDbDataAdapter(sqlCountry, conn)
				adapter.Fill(vendor)
			End Using
			sheet.InsertDataTable(vendor, True, 1, 1)

			'set the style of the new sheet
			sheet.AllocatedRange.AutoFitColumns()
			sheet.Range(1, 1, 1, vendor.Columns.Count).Style.Color = Color.DeepSkyBlue
			sheet.Range(1, 1, 1, vendor.Columns.Count).Style.Font.IsBold = True
			sheet.Range(1, 1, 1, vendor.Columns.Count).HorizontalAlignment = HorizontalAlignType.Center
			sheet.Range(2, 1, vendor.Rows.Count + 1, vendor.Columns.Count).Style.Color = Color.SkyBlue
			sheet.AllocatedRange.Style.Borders.LineStyle = LineStyleType.Thin
			sheet.AllocatedRange.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.AllocatedRange.Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace