Html export - export table html and css
Use the dropdowns to change table style and theme, then click the Reload button. This is the only roundtrip done back to EPPlus. Change the appearance of the table by checking/unchecking the checkboxes.
All CSS classes needed to style the table are exported from EPPlus and based on Excels table styles and themes. You can use the dev tools of your favorite browser to see how the classes are applied to the table.
Some selected parts of the model class for this page
public void SetupSampleData(int theme, TableStyles? style = TableStyles.Dark1)
{
// This method just fakes some data into a data table
InitDataTable();
using(var package = new ExcelPackage())
{
SetTheme(theme, package);
var sheet = package.Workbook.Worksheets.Add("Html export sample 1");
var tableRange = sheet.Cells["A1"].LoadFromDataTable(_dataTable, true, style);
// set number format for the BirthDate column
sheet.Cells[tableRange.Start.Row + 1, 4, tableRange.End.Row, 4].Style.Numberformat.Format = "yyyy-MM-dd";
tableRange.AutoFitColumns();
var table = sheet.Tables.GetFromRange(tableRange);
// table properties
table.ShowFirstColumn = this.ShowFirstColumn;
table.ShowLastColumn = this.ShowLastColumn;
table.ShowColumnStripes = this.ShowColumnStripes;
table.ShowRowStripes = this.ShowRowsStripes;
// Export Html and CSS
var exporter = table.CreateHtmlExporter();
exporter.Settings.Minify = false;
Css = exporter.GetCssString();
Html = exporter.GetHtmlString();
WorkbookBytes = package.GetAsByteArray();
}
}
private static void SetTheme(int theme, ExcelPackage package)
{
if (theme > 0)
{
var fileInfo = default(FileInfo);
switch (theme)
{
case 1:
fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"themes\\Ion.thmx"));
break;
case 2:
fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"themes\\Banded.thmx"));
break;
case 3:
fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"themes\\Parallax.thmx"));
break;
default:
fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"themes\\Ion.thmx"));
break;
}
package.Workbook.ThemeManager.Load(fileInfo);
}
}
public string Css { get; set; }
public string Html { get; set; }
Html as it was exported
<table class="epplus-table ts-dark1 ts-dark1-header" role="table">
<thead role="rowgroup">
<tr role="row">
<th data-datatype="string" class="epp-al" role="columnheader" scope="col">Country</th>
<th data-datatype="string" class="epp-al" role="columnheader" scope="col">FirstName</th>
<th data-datatype="string" class="epp-al" role="columnheader" scope="col">LastName</th>
<th data-datatype="datetime" class="epp-al" role="columnheader" scope="col">BirthDate</th>
<th data-datatype="string" class="epp-al" role="columnheader" scope="col">City</th>
</tr>
</thead>
<tbody role="rowgroup">
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Gustave</td>
<td role="cell">Robel</td>
<td data-value="1201219200000" role="cell" class="epp-ar">2008-01-25</td>
<td role="cell">Goyettemouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Luciano</td>
<td role="cell">Kulas</td>
<td data-value="-1206144000000" role="cell" class="epp-ar">1931-10-13</td>
<td role="cell">Eldredhaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Earl</td>
<td role="cell">Sanford</td>
<td data-value="-1175212800000" role="cell" class="epp-ar">1932-10-05</td>
<td role="cell">Jonatanberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Margret</td>
<td role="cell">Lueilwitz</td>
<td data-value="-1314489600000" role="cell" class="epp-ar">1928-05-07</td>
<td role="cell">North Roslynshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Noble</td>
<td role="cell">Kris</td>
<td data-value="-684288000000" role="cell" class="epp-ar">1948-04-26</td>
<td role="cell">Lake Alenestad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Abby</td>
<td role="cell">Schultz</td>
<td data-value="-151200000000" role="cell" class="epp-ar">1965-03-18</td>
<td role="cell">Torphyton</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Quinn</td>
<td role="cell">Mayer</td>
<td data-value="1106438400000" role="cell" class="epp-ar">2005-01-23</td>
<td role="cell">New Laisha</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Fiona</td>
<td role="cell">Hand</td>
<td data-value="-622771200000" role="cell" class="epp-ar">1950-04-08</td>
<td role="cell">Wuckertport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Kay</td>
<td role="cell">Collier</td>
<td data-value="-334022400000" role="cell" class="epp-ar">1959-06-02</td>
<td role="cell">Abbottchester</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Susana</td>
<td role="cell">Steuber</td>
<td data-value="-275443200000" role="cell" class="epp-ar">1961-04-10</td>
<td role="cell">Gradymouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Ruthie</td>
<td role="cell">Welch</td>
<td data-value="-1180396800000" role="cell" class="epp-ar">1932-08-06</td>
<td role="cell">North Monatown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Friedrich</td>
<td role="cell">Larkin</td>
<td data-value="-894153600000" role="cell" class="epp-ar">1941-09-01</td>
<td role="cell">Anibalborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Donny</td>
<td role="cell">Heaney</td>
<td data-value="-766540800000" role="cell" class="epp-ar">1945-09-17</td>
<td role="cell">Mckaylaport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Peyton</td>
<td role="cell">Abbott</td>
<td data-value="24192000000" role="cell" class="epp-ar">1970-10-08</td>
<td role="cell">East Odellview</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Adelle</td>
<td role="cell">Ryan</td>
<td data-value="1741392000000" role="cell" class="epp-ar">2025-03-08</td>
<td role="cell">Lake Cecelia</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Rachel</td>
<td role="cell">Goldner</td>
<td data-value="-1300147200000" role="cell" class="epp-ar">1928-10-20</td>
<td role="cell">Hoegerchester</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Andrew</td>
<td role="cell">Ryan</td>
<td data-value="1533600000000" role="cell" class="epp-ar">2018-08-07</td>
<td role="cell">Jordanemouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Janessa</td>
<td role="cell">Schiller</td>
<td data-value="-1669852800000" role="cell" class="epp-ar">1917-02-01</td>
<td role="cell">Lilahaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Lloyd</td>
<td role="cell">Roberts</td>
<td data-value="1370563200000" role="cell" class="epp-ar">2013-06-07</td>
<td role="cell">Pacochaville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Alexanne</td>
<td role="cell">Jaskolski</td>
<td data-value="38102400000" role="cell" class="epp-ar">1971-03-18</td>
<td role="cell">East Cynthia</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Jensen</td>
<td role="cell">Metz</td>
<td data-value="1779321600000" role="cell" class="epp-ar">2026-05-21</td>
<td role="cell">Ellenfort</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Ella</td>
<td role="cell">Hintz</td>
<td data-value="-1036800000" role="cell" class="epp-ar">1969-12-20</td>
<td role="cell">New Derektown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Odell</td>
<td role="cell">Veum</td>
<td data-value="526348800000" role="cell" class="epp-ar">1986-09-06</td>
<td role="cell">Schroederland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Manuela</td>
<td role="cell">Champlin</td>
<td data-value="1239235200000" role="cell" class="epp-ar">2009-04-09</td>
<td role="cell">South Gaybury</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Blake</td>
<td role="cell">Lockman</td>
<td data-value="931564800000" role="cell" class="epp-ar">1999-07-10</td>
<td role="cell">Schultzfort</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Ruby</td>
<td role="cell">Kassulke</td>
<td data-value="-180144000000" role="cell" class="epp-ar">1964-04-17</td>
<td role="cell">Lake Elda</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Taya</td>
<td role="cell">DuBuque</td>
<td data-value="-1281052800000" role="cell" class="epp-ar">1929-05-29</td>
<td role="cell">Swaniawskiside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Chris</td>
<td role="cell">Cremin</td>
<td data-value="-1736121600000" role="cell" class="epp-ar">1914-12-27</td>
<td role="cell">Darianaside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Jadon</td>
<td role="cell">Carroll</td>
<td data-value="1606780800000" role="cell" class="epp-ar">2020-12-01</td>
<td role="cell">North Merlport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Herbert</td>
<td role="cell">Maggio</td>
<td data-value="-152236800000" role="cell" class="epp-ar">1965-03-06</td>
<td role="cell">Batzton</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Aylin</td>
<td role="cell">Hilll</td>
<td data-value="158630400000" role="cell" class="epp-ar">1975-01-11</td>
<td role="cell">East Orlandohaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Ivy</td>
<td role="cell">Schumm</td>
<td data-value="775440000000" role="cell" class="epp-ar">1994-07-29</td>
<td role="cell">Bodeburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Candido</td>
<td role="cell">McKenzie</td>
<td data-value="-445651200000" role="cell" class="epp-ar">1955-11-18</td>
<td role="cell">Joshfurt</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Kristian</td>
<td role="cell">Feest</td>
<td data-value="-771120000000" role="cell" class="epp-ar">1945-07-26</td>
<td role="cell">Robelshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Adolfo</td>
<td role="cell">Mann</td>
<td data-value="-1130889600000" role="cell" class="epp-ar">1934-03-02</td>
<td role="cell">West Omariland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Rosella</td>
<td role="cell">Glover</td>
<td data-value="1524355200000" role="cell" class="epp-ar">2018-04-22</td>
<td role="cell">Port Nicklauston</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Marianna</td>
<td role="cell">Beatty</td>
<td data-value="-900633600000" role="cell" class="epp-ar">1941-06-18</td>
<td role="cell">Spencerfurt</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Eliseo</td>
<td role="cell">Hand</td>
<td data-value="-501984000000" role="cell" class="epp-ar">1954-02-04</td>
<td role="cell">East Fay</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Emile</td>
<td role="cell">Walter</td>
<td data-value="898819200000" role="cell" class="epp-ar">1998-06-26</td>
<td role="cell">Ulisesshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Cody</td>
<td role="cell">Pollich</td>
<td data-value="-1305849600000" role="cell" class="epp-ar">1928-08-15</td>
<td role="cell">South Nathanside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Mack</td>
<td role="cell">Klein</td>
<td data-value="-2016662400000" role="cell" class="epp-ar">1906-02-05</td>
<td role="cell">Wizachester</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Vallie</td>
<td role="cell">Wintheiser</td>
<td data-value="-387504000000" role="cell" class="epp-ar">1957-09-21</td>
<td role="cell">Allanberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Mya</td>
<td role="cell">Keeling</td>
<td data-value="-1493164800000" role="cell" class="epp-ar">1922-09-08</td>
<td role="cell">Sashaborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Keon</td>
<td role="cell">Bechtelar</td>
<td data-value="-1303603200000" role="cell" class="epp-ar">1928-09-10</td>
<td role="cell">Arliemouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Pamela</td>
<td role="cell">O'Reilly</td>
<td data-value="-593049600000" role="cell" class="epp-ar">1951-03-18</td>
<td role="cell">East Keithville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Queenie</td>
<td role="cell">Bogisich</td>
<td data-value="944784000000" role="cell" class="epp-ar">1999-12-10</td>
<td role="cell">Daughertytown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Brielle</td>
<td role="cell">Batz</td>
<td data-value="-476755200000" role="cell" class="epp-ar">1954-11-23</td>
<td role="cell">Port Leann</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Saul</td>
<td role="cell">Emmerich</td>
<td data-value="-771897600000" role="cell" class="epp-ar">1945-07-17</td>
<td role="cell">Aryannaport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Dillan</td>
<td role="cell">Miller</td>
<td data-value="-245635200000" role="cell" class="epp-ar">1962-03-21</td>
<td role="cell">New Filomena</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Owen</td>
<td role="cell">Boyle</td>
<td data-value="748915200000" role="cell" class="epp-ar">1993-09-25</td>
<td role="cell">Willisville</td>
</tr>
</tbody>
</table>
EPPlus converts the table styling in Excel to a separate stylesheet.
table.epplus-table{
font-family:Calibri;
font-size:11pt;
border-spacing:0;
border-collapse:collapse;
word-wrap:break-word;
white-space:nowrap;
}
.epp-hidden {
display:none;
}
.epp-al {
text-align:left;
}
.epp-ar {
text-align:right;
}
.epp-dcw {
width:64px;
}
.epp-drh {
height:20px;
}
table.epplus-table.ts-dark1 a{
color:#ffffff;
}
table.epplus-table.ts-dark1 td:nth-child(4){
text-align:right;
}
table.epplus-table.ts-dark1{
background-color:#737373;
color:#ffffff;
}
table.epplus-table.ts-dark1 thead{
background-color:#000000;
color:#ffffff;
font-weight:bolder;
border-bottom:medium solid #ffffff;
}
table.epplus-table.ts-dark1 tfoot{
background-color:#262626;
color:#ffffff;
font-weight:bolder;
border-top:medium solid #ffffff;
}
table.epplus-table.ts-dark1-column-stripes tbody tr td:nth-child(odd){
background-color:#404040;
}
table.epplus-table.ts-dark1-row-stripes tbody tr:nth-child(odd){
background-color:#404040;
}
table.epplus-table.ts-dark1-last-column tbody tr td:last-child{
background-color:#404040;
color:#ffffff;
font-weight:bolder;
border-left:medium solid #ffffff;
}
table.epplus-table.ts-dark1-first-column tbody tr td:first-child{
background-color:#404040;
color:#ffffff;
font-weight:bolder;
border-right:medium solid #ffffff;
}