Convert an Excel table to a range from your C#/VB.NET applications

The following example shows how you can convert an Excel table to a range by using only the GemBox.Spreadsheet .NET component.

Steps for converting an Excel table to a range

You can do a basic conversion with just a few lines of code. You only need an IDE like Visual Studio and .NET Framework, .NET Core, or .NET Standard.

Follow these steps to convert from one file format to another:

  • Create or use an existing C# or VB.NET project.
  • Download and install GemBox.Spreadsheet Setup.
  • Add a reference to GemBox.Spreadsheet.dll within your C# or VB.NET project.
  • Optionally, you can add GemBox.Spreadsheet Nuget package to your C# or VB.NET project.
  • Execute the C#/VB.NET code below.

Convert Excel tables online

You can test converting your Excel tables with the interactive example below. Just upload your file that contains a table, choose output file format, click Run Example, and download the generated file.

The C#/VB.NET code will be compiled, and your file will be converted with only .NET framework and the GemBox.Spreadsheet component.

The code copies the table's header and data without formatting, and removes the original table.

Screenshot of Excel file created by converting table to range
Screenshot of Excel file created by converting table to range
Upload your file (Drag file here)
using System;
using System.Linq;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Tables;

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

        ExcelFile workbook = ExcelFile.Load("%InputFileName%");

        // Iterate through all sheets and tables
        foreach (ExcelWorksheet sheet in workbook.Worksheets)
        {
            foreach (Table table in sheet.Tables.ToArray())
            {
                // Make space to copy data
                table.Range.Insert(InsertShiftDirection.Right);
                // Copy header row
                table.Range.GetSubrangeRelative(0, 0, table.Range.Width, 1).CopyTo(table.Range.FirstRowIndex, table.Range.FirstColumnIndex - table.Range.Width);
                // Copy data
                table.DataRange.CopyTo(table.Range.FirstRowIndex + 1, table.Range.FirstColumnIndex - table.Range.Width);

                // Remove table
                sheet.Tables.Remove(table, RemoveShiftDirection.Left);
            }
        }

        workbook.Save("ExcelTableToRange.%OutputFileType%");
    }
}
Imports System
Imports System.Linq
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Tables

Module Program
    Sub Main()

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

        Dim workbook As ExcelFile = ExcelFile.Load("%InputFileName%")

        ' Iterate through all sheets And tables
        For Each sheet As ExcelWorksheet In workbook.Worksheets
            For Each table As Table In sheet.Tables.ToArray()
                ' Make space to copy data
                table.Range.Insert(InsertShiftDirection.Right)
                ' Copy header row
                table.Range.GetSubrangeRelative(0, 0, table.Range.Width, 1).CopyTo(table.Range.FirstRowIndex, table.Range.FirstColumnIndex - table.Range.Width)
                ' Copy data
                table.DataRange.CopyTo(table.Range.FirstRowIndex + 1, table.Range.FirstColumnIndex - table.Range.Width)
                ' Remove table
                sheet.Tables.Remove(table, RemoveShiftDirection.Left)
            Next
        Next

        workbook.Save("ExcelTableToRange.%OutputFileType%")
    End Sub
End Module

Support for Excel tables

GemBox.Spreadsheet supports Excel tables for the XLSX file format only. To see more about support for tables, check the Create Excel table example.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.