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 or JetBrains Rider and .NET Framework, .NET Core, or any other platform that implements .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.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy