Skip to content

Built in Table styles

EPPlus provides approximately 60 different built in Table styles.
These are defined in the OfficeOpenXml.TableStyles enum.
The table styles defines background colors, borders, etc for the various elements of a table.
This program below generates a workbook where you can see all these table styles - run the program once and then you can explore the different table styles in the workbook.

The program is a console program that needs to have a reference to the EPPlus nuget package.

using OfficeOpenXml;
using OfficeOpenXml.Table;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AllTableStyles
{
    class Program
    {
        // this is the path of the generated workbook. Change it to whatever you like.
        const string FilePath = @"c:\Temp\AllTableStyles.xlsx";

        static IEnumerable<TableStyles> GetTableStyles()
        {
            return Enum.GetValues(typeof(TableStyles)).Cast<TableStyles>();
        }

        static void PrintTable(ExcelWorksheet worksheet, int row, int col, TableStyles style)
        {
            var table = worksheet.Tables.Add(worksheet.Cells[row, col, row + 4, col + 3], "");
            // here you can test to change some other properties of the table, such as:
            //table.ShowFirstColumn = true;
            //table.ShowRowStripes = true;
            table.TableStyle = style;

            var c1 = table.Columns.Count();
            c1++;
            var range = table.Columns.Add(1);
            for(var ix = range.Start.Row; ix < range.End.Row; ix++)
            {
                worksheet.SetValue(ix, c1, "abc");
            }

            for(var c = table.Range.Start.Column; c < table.Range.End.Column; c++)
            {
                for (var r = table.Range.Start.Row + 1; r < table.Range.End.Row;  r++)
                {
                    worksheet.Cells[r, c].Value = c + r;
                }
            }
        }

        static void Main(string[] args)
        {
            ExcelPackage.LicenseContext = LicenseContext.Commercial;
            // Or if you are using EPPlus in a NonCommercial context:
            // ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using(var package = new ExcelPackage(new FileInfo(FilePath)))
            {
                var ws = package.Workbook.Worksheets.Add("All styles");
                var row = 1;
                var col = 1;
                var i = 0;
                var styles = GetTableStyles();
                foreach (var style in styles)
                {
                    ws.Cells[row, col].Value = style.ToString();
                    ws.Cells[row, col].Style.Font.Bold = true;
                    PrintTable(ws, row + 1, col, style);
                    if(++i % 3 == 0)
                    {
                        row += 7;
                        col = 1;
                    }
                    else
                    {
                        col += 6;
                    }
                }
                package.Save();
            }
            Console.WriteLine("Done!");
            Console.ReadKey();
        }
    }
}
Imports OfficeOpenXml
Imports OfficeOpenXml.Table
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

Namespace AllTableStyles
    Friend Class Program
        ' this is the path of the generated workbook. Change it to whatever you like.
        Const FilePath As String = "c:\Temp\AllTableStyles.xlsx"

        Private Shared Function GetTableStyles() As IEnumerable(Of TableStyles)
            Return [Enum].GetValues(GetType(TableStyles)).Cast(Of TableStyles)()
        End Function

        Private Shared Sub PrintTable(worksheet As ExcelWorksheet, row As Integer, col As Integer, style As TableStyles)
            Dim table = worksheet.Tables.Add(worksheet.Cells(row, col, row + 4, col + 3), "")
            ' here you can test to change some other properties of the table, such as:
            'table.ShowFirstColumn = true;
            'table.ShowRowStripes = true;
            table.TableStyle = style

            Dim c1 = table.Columns.Count()
            c1 += 1
            Dim range = table.Columns.Add(1)
            For ix As Integer = range.Start.Row To range.End.Row - 1
                worksheet.SetValue(ix, c1, "abc")
            Next

            For c As Integer = table.Range.Start.Column To table.Range.End.Column - 1
                For r As Integer = table.Range.Start.Row + 1 To table.Range.End.Row - 1
                    worksheet.Cells(r, c).Value = c + r
                Next
            Next
        End Sub

        Shared Sub Main(args As String())
            ExcelPackage.LicenseContext = LicenseContext.Commercial
            ' Or if you are using EPPlus in a NonCommercial context:
            ' ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            Using package As New ExcelPackage(New FileInfo(FilePath))
                Dim ws = package.Workbook.Worksheets.Add("All styles")
                Dim row = 1
                Dim col = 1
                Dim i = 0
                Dim styles = GetTableStyles()
                For Each style In styles
                    ws.Cells(row, col).Value = style.ToString()
                    ws.Cells(row, col).Style.Font.Bold = True
                    PrintTable(ws, row + 1, col, style)
                    i += 1
                    If i Mod 3 = 0 Then
                        row += 7
                        col = 1
                    Else
                        col += 6
                    End If
                Next
                package.Save()
            End Using
            Console.WriteLine("Done!")
            Console.ReadKey()
        End Sub
    End Class
End Namespace

See also