C#: Create, Resize or Remove Tables in Excel

A table in Excel is a structured range of data that includes headers for each column. When you convert a range of cells into a table, Excel automatically applies formatting, adds filter arrows to each header cell, and provides enhanced features for manipulating and analyzing the data. In this article, we will explain how to create, resize, and remove tables in Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Create a Table in Excel in C#

Spire.XLS for .NET allows you to convert a specific range of data in an Excel worksheet to a table using the Worksheet.ListObjects.Create(tableName, cellRange) method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Get the cell range you want to convert to a table using Worksheet.Range[] property.
  • Convert the cell range to a table using Worksheet.ListObjects.Create(tableName, cellRange) method.
  • Save the resulting workbook to a file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core;

namespace CreateTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the cell range you want to convert to a table
            CellRange range = sheet.Range[1, 1, sheet.LastRow, sheet.LastColumn];

            //Convert the cell range to a table
            IListObject table = sheet.ListObjects.Create("SalesTransactions", range);

            //Format the table with a built-in table style
            table.BuiltInTableStyle = TableBuiltInStyles.TableStyleLight2;

            //Save the resulting workbook to a file
            workbook.SaveToFile("CreateTable.xlsx", ExcelVersion.Version2016);
            workbook.Dispose();
        }
    }
}

C#: Create, Resize or Remove Tables in Excel

Add a Total Row to a Table in Excel in C#

You can add a total row after the end of a table to display summary calculations, such as sums, averages, or other aggregations of the data in the table. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Get a specific table in the worksheet using Worksheet.ListObjects[index] property.
  • Display a total row at the end of the table by setting Table.DisplayTotalRow property to true.
  • Set total row label in a specific table column using IListObject.Columns[index].TotalsRowLabel property.
  • Set the calculation functions for specific table columns using IListObject.Columns[index].TotalsCalculation property.
  • Save the resulting workbook to a file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core;

namespace AddTotalRowToTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("CreateTable.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the first table in the worksheet
            IListObject table = sheet.ListObjects[0];

            //Show total row
            table.DisplayTotalRow = true;
            // Set total row label
            table.Columns[0].TotalsRowLabel = "Total";
            //Set the function used for the total calculation
            table.Columns[3].TotalsCalculation = ExcelTotalsCalculation.Sum;
            table.Columns[4].TotalsCalculation = ExcelTotalsCalculation.Sum;

            //Save the resulting workbook to a file
            workbook.SaveToFile("AddTotalRow.xlsx", ExcelVersion.Version2016);
            workbook.Dispose();            
        }
    }
}

C#: Create, Resize or Remove Tables in Excel

Resize a Table in Excel in C#

You can resize a table by updating the data range of it using IListObject.Location property. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Get a specific table in the worksheet using Worksheet.ListObjects[index] property.
  • Resize the table by updating the data range of it using IListObject.Location property.
  • Save the resulting workbook to a file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core;

namespace ResizeTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("CreateTable.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the first table in the worksheet
            IListObject table = sheet.ListObjects[0];

            table.Location = sheet.Range["C1:E8"];

            //Save the resulting workbook to a file
            workbook.SaveToFile("ResizeTable.xlsx", ExcelVersion.Version2016);
            workbook.Dispose();
        }
    }
}

C#: Create, Resize or Remove Tables in Excel

Remove a Table from Excel in C#

If you no longer need a table, you can convert it back to a normal range of cells by using the IListObjects.RemoveAt(tableIndex) method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Get the table collection of the worksheet using Worksheet.ListObjects property.
  • Remove a specific table from the table collection using IListObjects.RemoveAt(tableIndex) property.
  • Save the resulting workbook to a file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core;

namespace RemoveTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("CreateTable.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the table collection of the worksheet
            IListObjects tables = sheet.ListObjects;
            //Remove a specific table by its index
            tables.RemoveAt(0);

            ////Or remove a specific table by its name
            //for (int i = tables.Count - 1; i >= 0; i--)
            //{
            //    // Check if the table name matches the specific value
            //    if (tables[i].Name == "SalesTransactions")
            //    {
            //        // Remove the table
            //        tables.RemoveAt(i);
            //    }
            //}

            //Save the resulting workbook to a file
            workbook.SaveToFile("RemoveTable.xlsx", ExcelVersion.Version2016);
            workbook.Dispose();
            System.Diagnostics.Process.Start("RemoveTable.xlsx");
        }
    }
}

C#: Create, Resize or Remove Tables in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.