Create Excel Tables in C# and VB.NET

Following example shows how to create Table from a range of cells in an Excel file with C# and VB.NET.

Tables are loaded from and saved to XLSX file format only.

Tables Screenshot
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Tables;

class Program
{
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("Tables");

        // Add some data
        object[,] data = new object[5, 3]
        {
            { "Worker", "Hours", "Price" },
            { "John Doe", 25, 35.0 },
            { "Jane Doe", 27, 35.0 },
            { "Jack White", 18, 32.0 },
            { "George Black", 31, 35.0 }
        };

        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 3; j++)
                ws.Cells[i, j].Value = data[i, j];

        // Set column widths
        ws.Columns[0].SetWidth(100, LengthUnit.Pixel);
        ws.Columns[1].SetWidth(70, LengthUnit.Pixel);
        ws.Columns[2].SetWidth(70, LengthUnit.Pixel);
        ws.Columns[3].SetWidth(70, LengthUnit.Pixel);
        ws.Columns[2].Style.NumberFormat = "\"$\"#,##0.00";
        ws.Columns[3].Style.NumberFormat = "\"$\"#,##0.00";

        // Create table and enable totals row
        Table table = ws.Tables.Add("Table1", "A1:C5", true);
        table.HasTotalsRow = true;

        // Add new column
        TableColumn column = table.Columns.Add();
        column.Name = "Total";

        // Populate column
        foreach (var cell in column.DataRange)
            cell.Formula = "=Table1[Hours] * Table1[Price]";

        // Set totals row function for newly added column and calculate it
        column.TotalsRowFunction = TotalsRowFunction.Sum;
        column.Range.Calculate();

        // Set table style
        table.BuiltInStyle = BuiltInTableStyleName.TableStyleMedium2;

        ef.Save("Tables.xlsx");
    }
}
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Tables

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim ef As ExcelFile = New ExcelFile
        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Tables")

        Dim data(,) As Object = New Object(4, 2) _
        {
            {"Worker", "Hours", "Price"},
            {"John Doe", 25, 35.0},
            {"Jane Doe", 27, 35.0},
            {"Jack White", 18, 32.0},
            {"George Black", 31, 35.0}
        }

        For i As Integer = 0 To 4
            For j As Integer = 0 To 2
                ws.Cells.Item(i, j).Value = data(i, j)
            Next
        Next

        ' Set column widths and formats
        ws.Columns(0).SetWidth(100, LengthUnit.Pixel)
        ws.Columns(1).SetWidth(70, LengthUnit.Pixel)
        ws.Columns(2).SetWidth(70, LengthUnit.Pixel)
        ws.Columns(3).SetWidth(70, LengthUnit.Pixel)
        ws.Columns(2).Style.NumberFormat = """$""#,##0.00"
        ws.Columns(3).Style.NumberFormat = """$""#,##0.00"

        ' Create table And enable totals row
        Dim table As Table = ws.Tables.Add("Table1", "A1:C5", True)
        table.HasTotalsRow = True

        ' Add New column
        Dim column As TableColumn = table.Columns.Add()
        column.Name = "Total"

        ' Populate column
        For Each cell In column.DataRange
            cell.Formula = "=Table1[Hours] * Table1[Price]"
        Next

        ' Set totals row function for newly added column and calculate it
        column.TotalsRowFunction = TotalsRowFunction.Sum
        column.Range.Calculate()

        ' Set table style
        table.BuiltInStyle = BuiltInTableStyleName.TableStyleMedium2

        ef.Save("Tables.xlsx")

    End Sub

End Module

Check next example or download examples from GitHub.