Skip to content

Built in Table styles

Mats Alm edited this page Jan 30, 2024 · 4 revisions

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();
        }
    }
}

See also

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally