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">England</td>
<td role="cell">Carolyne</td>
<td role="cell">Beatty</td>
<td data-value="1554163200000" role="cell" class="epp-ar">2019-04-02</td>
<td role="cell">Emmamouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Bernie</td>
<td role="cell">Robel</td>
<td data-value="-1527033600000" role="cell" class="epp-ar">1921-08-12</td>
<td role="cell">Sporerside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Trenton</td>
<td role="cell">McCullough</td>
<td data-value="299289600000" role="cell" class="epp-ar">1979-06-27</td>
<td role="cell">Makennastad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Jonathan</td>
<td role="cell">Runte</td>
<td data-value="-652492800000" role="cell" class="epp-ar">1949-04-29</td>
<td role="cell">Port Tobyview</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Kavon</td>
<td role="cell">Boyle</td>
<td data-value="1207526400000" role="cell" class="epp-ar">2008-04-07</td>
<td role="cell">Lynchburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Delaney</td>
<td role="cell">Howell</td>
<td data-value="1237593600000" role="cell" class="epp-ar">2009-03-21</td>
<td role="cell">South Enochville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Lewis</td>
<td role="cell">Doyle</td>
<td data-value="-236044800000" role="cell" class="epp-ar">1962-07-10</td>
<td role="cell">Lake Karianne</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Levi</td>
<td role="cell">Rath</td>
<td data-value="1073174400000" role="cell" class="epp-ar">2004-01-04</td>
<td role="cell">New Erica</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Pete</td>
<td role="cell">Rogahn</td>
<td data-value="-187056000000" role="cell" class="epp-ar">1964-01-28</td>
<td role="cell">South Patrickburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Ericka</td>
<td role="cell">Walker</td>
<td data-value="-827798400000" role="cell" class="epp-ar">1943-10-09</td>
<td role="cell">North Thaddeustown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Alphonso</td>
<td role="cell">Cruickshank</td>
<td data-value="-1438646400000" role="cell" class="epp-ar">1924-05-31</td>
<td role="cell">North Laylaview</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Retha</td>
<td role="cell">Hudson</td>
<td data-value="1546041600000" role="cell" class="epp-ar">2018-12-29</td>
<td role="cell">Delphastad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Lavada</td>
<td role="cell">Spinka</td>
<td data-value="-719798400000" role="cell" class="epp-ar">1947-03-12</td>
<td role="cell">Josefinafurt</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Elyssa</td>
<td role="cell">Brekke</td>
<td data-value="1314230400000" role="cell" class="epp-ar">2011-08-25</td>
<td role="cell">Lake Quintenstad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Reta</td>
<td role="cell">Lind</td>
<td data-value="87264000000" role="cell" class="epp-ar">1972-10-07</td>
<td role="cell">East Jazlynview</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Patsy</td>
<td role="cell">Lehner</td>
<td data-value="-1984953600000" role="cell" class="epp-ar">1907-02-07</td>
<td role="cell">Port Buster</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Keira</td>
<td role="cell">Gaylord</td>
<td data-value="1545609600000" role="cell" class="epp-ar">2018-12-24</td>
<td role="cell">North Frederic</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Michele</td>
<td role="cell">Oberbrunner</td>
<td data-value="25747200000" role="cell" class="epp-ar">1970-10-26</td>
<td role="cell">Handville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Cortney</td>
<td role="cell">Waelchi</td>
<td data-value="-210902400000" role="cell" class="epp-ar">1963-04-27</td>
<td role="cell">Cliftonland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Lauriane</td>
<td role="cell">Gleason</td>
<td data-value="-1418515200000" role="cell" class="epp-ar">1925-01-19</td>
<td role="cell">Kossburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Rudy</td>
<td role="cell">Smith</td>
<td data-value="835574400000" role="cell" class="epp-ar">1996-06-24</td>
<td role="cell">Carolinafort</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Clare</td>
<td role="cell">Kerluke</td>
<td data-value="-762048000000" role="cell" class="epp-ar">1945-11-08</td>
<td role="cell">Hamillmouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Katlyn</td>
<td role="cell">Hintz</td>
<td data-value="-1833580800000" role="cell" class="epp-ar">1911-11-25</td>
<td role="cell">Christiansenberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Eldred</td>
<td role="cell">Konopelski</td>
<td data-value="412560000000" role="cell" class="epp-ar">1983-01-28</td>
<td role="cell">Lake Angie</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Jeanie</td>
<td role="cell">Bins</td>
<td data-value="961804800000" role="cell" class="epp-ar">2000-06-24</td>
<td role="cell">North Kadinburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Amira</td>
<td role="cell">Boyer</td>
<td data-value="-806716800000" role="cell" class="epp-ar">1944-06-09</td>
<td role="cell">Botsfordberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Sibyl</td>
<td role="cell">Lockman</td>
<td data-value="-34041600000" role="cell" class="epp-ar">1968-12-03</td>
<td role="cell">Bashirianburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Chris</td>
<td role="cell">Harvey</td>
<td data-value="-1691280000000" role="cell" class="epp-ar">1916-05-29</td>
<td role="cell">Deondrefort</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">August</td>
<td role="cell">Treutel</td>
<td data-value="-2030918400000" role="cell" class="epp-ar">1905-08-24</td>
<td role="cell">Port Freeman</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Cedrick</td>
<td role="cell">Kreiger</td>
<td data-value="661046400000" role="cell" class="epp-ar">1990-12-13</td>
<td role="cell">Mavisstad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Nelson</td>
<td role="cell">Herman</td>
<td data-value="-936748800000" role="cell" class="epp-ar">1940-04-26</td>
<td role="cell">Creminmouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Osvaldo</td>
<td role="cell">Stanton</td>
<td data-value="-2157321600000" role="cell" class="epp-ar">1901-08-22</td>
<td role="cell">Zulaufmouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Cristal</td>
<td role="cell">Murazik</td>
<td data-value="915580800000" role="cell" class="epp-ar">1999-01-06</td>
<td role="cell">Port Rettatown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Eldon</td>
<td role="cell">Zemlak</td>
<td data-value="695174400000" role="cell" class="epp-ar">1992-01-12</td>
<td role="cell">Borerstad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Eva</td>
<td role="cell">Schroeder</td>
<td data-value="1175040000000" role="cell" class="epp-ar">2007-03-28</td>
<td role="cell">Sylviaburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Orland</td>
<td role="cell">Heidenreich</td>
<td data-value="-540345600000" role="cell" class="epp-ar">1952-11-17</td>
<td role="cell">West Cordeliahaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Cara</td>
<td role="cell">Armstrong</td>
<td data-value="309139200000" role="cell" class="epp-ar">1979-10-19</td>
<td role="cell">Lake Vicentemouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Alene</td>
<td role="cell">Schinner</td>
<td data-value="-1599782400000" role="cell" class="epp-ar">1919-04-23</td>
<td role="cell">Kerlukeshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Rudolph</td>
<td role="cell">Hansen</td>
<td data-value="245030400000" role="cell" class="epp-ar">1977-10-07</td>
<td role="cell">Spencerton</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Alvis</td>
<td role="cell">Stoltenberg</td>
<td data-value="248054400000" role="cell" class="epp-ar">1977-11-11</td>
<td role="cell">Lake Lazarofort</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Estrella</td>
<td role="cell">Wolf</td>
<td data-value="524880000000" role="cell" class="epp-ar">1986-08-20</td>
<td role="cell">Port Easton</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Abner</td>
<td role="cell">Monahan</td>
<td data-value="-1633910400000" role="cell" class="epp-ar">1918-03-24</td>
<td role="cell">West Maverick</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Ashley</td>
<td role="cell">Hilll</td>
<td data-value="-213321600000" role="cell" class="epp-ar">1963-03-30</td>
<td role="cell">West Chanelshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Harrison</td>
<td role="cell">Durgan</td>
<td data-value="389318400000" role="cell" class="epp-ar">1982-05-04</td>
<td role="cell">Lake Isaiasport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Benedict</td>
<td role="cell">Predovic</td>
<td data-value="1138233600000" role="cell" class="epp-ar">2006-01-26</td>
<td role="cell">Keelyburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Adela</td>
<td role="cell">Bode</td>
<td data-value="-1416009600000" role="cell" class="epp-ar">1925-02-17</td>
<td role="cell">Beerfurt</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Felix</td>
<td role="cell">Leuschke</td>
<td data-value="910051200000" role="cell" class="epp-ar">1998-11-03</td>
<td role="cell">South Angelmouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Uriah</td>
<td role="cell">Little</td>
<td data-value="507600000000" role="cell" class="epp-ar">1986-02-01</td>
<td role="cell">East Elvis</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Gregoria</td>
<td role="cell">Rolfson</td>
<td data-value="-604972800000" role="cell" class="epp-ar">1950-10-31</td>
<td role="cell">Geovanyshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Icie</td>
<td role="cell">Pfeffer</td>
<td data-value="561772800000" role="cell" class="epp-ar">1987-10-21</td>
<td role="cell">Lockmantown</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;
}