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">Scotland</td>
<td role="cell">Zechariah</td>
<td role="cell">Schmitt</td>
<td data-value="-513820800000" role="cell" class="epp-ar epp-s1">1953-09-20</td>
<td role="cell">Powlowskishire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Joannie</td>
<td role="cell">Roberts</td>
<td data-value="420854400000" role="cell" class="epp-ar epp-s1">1983-05-04</td>
<td role="cell">Cormierland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Bessie</td>
<td role="cell">Schuppe</td>
<td data-value="-1002931200000" role="cell" class="epp-ar epp-s1">1938-03-22</td>
<td role="cell">Port Shad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Alisha</td>
<td role="cell">Grant</td>
<td data-value="278467200000" role="cell" class="epp-ar epp-s1">1978-10-29</td>
<td role="cell">Port Anabelleland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Estell</td>
<td role="cell">Bahringer</td>
<td data-value="-540864000000" role="cell" class="epp-ar epp-s1">1952-11-11</td>
<td role="cell">Hirtheburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Claudia</td>
<td role="cell">Stehr</td>
<td data-value="-1341705600000" role="cell" class="epp-ar epp-s1">1927-06-27</td>
<td role="cell">West Kyraside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Godfrey</td>
<td role="cell">Morissette</td>
<td data-value="-2004220800000" role="cell" class="epp-ar epp-s1">1906-06-29</td>
<td role="cell">Lowetown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Mack</td>
<td role="cell">Legros</td>
<td data-value="-516758400000" role="cell" class="epp-ar epp-s1">1953-08-17</td>
<td role="cell">East Andreaneville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Alaina</td>
<td role="cell">Keeling</td>
<td data-value="-1729036800000" role="cell" class="epp-ar epp-s1">1915-03-19</td>
<td role="cell">New Vince</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Heidi</td>
<td role="cell">Jacobi</td>
<td data-value="760406400000" role="cell" class="epp-ar epp-s1">1994-02-05</td>
<td role="cell">East Uriahville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Alaina</td>
<td role="cell">Torphy</td>
<td data-value="760838400000" role="cell" class="epp-ar epp-s1">1994-02-10</td>
<td role="cell">Abdullahport</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Michelle</td>
<td role="cell">Schuster</td>
<td data-value="-177638400000" role="cell" class="epp-ar epp-s1">1964-05-16</td>
<td role="cell">Rippinstad</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Wiley</td>
<td role="cell">McDermott</td>
<td data-value="67651200000" role="cell" class="epp-ar epp-s1">1972-02-23</td>
<td role="cell">Predovicchester</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Mariano</td>
<td role="cell">O'Kon</td>
<td data-value="1166313600000" role="cell" class="epp-ar epp-s1">2006-12-17</td>
<td role="cell">Cesarchester</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Mertie</td>
<td role="cell">Howe</td>
<td data-value="-1876348800000" role="cell" class="epp-ar epp-s1">1910-07-18</td>
<td role="cell">Troyborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Favian</td>
<td role="cell">Reynolds</td>
<td data-value="-1582329600000" role="cell" class="epp-ar epp-s1">1919-11-11</td>
<td role="cell">New Noemi</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Uriah</td>
<td role="cell">Marquardt</td>
<td data-value="346377600000" role="cell" class="epp-ar epp-s1">1980-12-23</td>
<td role="cell">Nyasiaborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Hank</td>
<td role="cell">McClure</td>
<td data-value="513907200000" role="cell" class="epp-ar epp-s1">1986-04-15</td>
<td role="cell">North Michelle</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Nikko</td>
<td role="cell">Gutkowski</td>
<td data-value="-136425600000" role="cell" class="epp-ar epp-s1">1965-09-05</td>
<td role="cell">Hilllberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Mariah</td>
<td role="cell">Leffler</td>
<td data-value="1018742400000" role="cell" class="epp-ar epp-s1">2002-04-14</td>
<td role="cell">New Abby</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Jada</td>
<td role="cell">Kovacek</td>
<td data-value="-309139200000" role="cell" class="epp-ar epp-s1">1960-03-16</td>
<td role="cell">North Ignatiusville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Derick</td>
<td role="cell">Shields</td>
<td data-value="1225670400000" role="cell" class="epp-ar epp-s1">2008-11-03</td>
<td role="cell">New Brenttown</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Sophia</td>
<td role="cell">Durgan</td>
<td data-value="-145756800000" role="cell" class="epp-ar epp-s1">1965-05-20</td>
<td role="cell">West Heaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Eric</td>
<td role="cell">Terry</td>
<td data-value="16761600000" role="cell" class="epp-ar epp-s1">1970-07-14</td>
<td role="cell">Jairomouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Gerson</td>
<td role="cell">Bergstrom</td>
<td data-value="-259200000" role="cell" class="epp-ar epp-s1">1969-12-29</td>
<td role="cell">East Georgette</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Erich</td>
<td role="cell">Hane</td>
<td data-value="1121558400000" role="cell" class="epp-ar epp-s1">2005-07-17</td>
<td role="cell">East Asa</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Dante</td>
<td role="cell">Wintheiser</td>
<td data-value="802828800000" role="cell" class="epp-ar epp-s1">1995-06-11</td>
<td role="cell">Fadelborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Reanna</td>
<td role="cell">Rohan</td>
<td data-value="1433462400000" role="cell" class="epp-ar epp-s1">2015-06-05</td>
<td role="cell">South Cydneyborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Angelica</td>
<td role="cell">Koepp</td>
<td data-value="1221091200000" role="cell" class="epp-ar epp-s1">2008-09-11</td>
<td role="cell">West Darby</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Eveline</td>
<td role="cell">Dicki</td>
<td data-value="683942400000" role="cell" class="epp-ar epp-s1">1991-09-04</td>
<td role="cell">Lowehaven</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Madalyn</td>
<td role="cell">Bednar</td>
<td data-value="-579916800000" role="cell" class="epp-ar epp-s1">1951-08-17</td>
<td role="cell">Arnoldoside</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Randy</td>
<td role="cell">Schmeler</td>
<td data-value="419212800000" role="cell" class="epp-ar epp-s1">1983-04-15</td>
<td role="cell">Brekkebury</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Elenora</td>
<td role="cell">Kerluke</td>
<td data-value="-705369600000" role="cell" class="epp-ar epp-s1">1947-08-26</td>
<td role="cell">South Chetburgh</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Austin</td>
<td role="cell">Gleichner</td>
<td data-value="-1680912000000" role="cell" class="epp-ar epp-s1">1916-09-26</td>
<td role="cell">Port Tamarabury</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Korey</td>
<td role="cell">Streich</td>
<td data-value="-1639008000000" role="cell" class="epp-ar epp-s1">1918-01-24</td>
<td role="cell">Blockbury</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Savanah</td>
<td role="cell">Kihn</td>
<td data-value="-494726400000" role="cell" class="epp-ar epp-s1">1954-04-29</td>
<td role="cell">Lindmouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Lenore</td>
<td role="cell">Stark</td>
<td data-value="-1619308800000" role="cell" class="epp-ar epp-s1">1918-09-09</td>
<td role="cell">Wellingtonberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Loren</td>
<td role="cell">Reynolds</td>
<td data-value="912297600000" role="cell" class="epp-ar epp-s1">1998-11-29</td>
<td role="cell">Matildaberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Alek</td>
<td role="cell">Mills</td>
<td data-value="-153360000000" role="cell" class="epp-ar epp-s1">1965-02-21</td>
<td role="cell">South Valentine</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Tito</td>
<td role="cell">Kovacek</td>
<td data-value="-191548800000" role="cell" class="epp-ar epp-s1">1963-12-07</td>
<td role="cell">Kristyberg</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Wales</td>
<td role="cell">Brycen</td>
<td role="cell">Smith</td>
<td data-value="-582336000000" role="cell" class="epp-ar epp-s1">1951-07-20</td>
<td role="cell">West Baileeville</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Ari</td>
<td role="cell">Bins</td>
<td data-value="-151545600000" role="cell" class="epp-ar epp-s1">1965-03-14</td>
<td role="cell">West Bretland</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Taurean</td>
<td role="cell">Kilback</td>
<td data-value="-421459200000" role="cell" class="epp-ar epp-s1">1956-08-24</td>
<td role="cell">Jastshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Janelle</td>
<td role="cell">Goyette</td>
<td data-value="-136166400000" role="cell" class="epp-ar epp-s1">1965-09-08</td>
<td role="cell">Westleyborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Christine</td>
<td role="cell">Botsford</td>
<td data-value="555292800000" role="cell" class="epp-ar epp-s1">1987-08-07</td>
<td role="cell">Hendersonview</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Northern Ireland</td>
<td role="cell">Richie</td>
<td role="cell">Hahn</td>
<td data-value="856915200000" role="cell" class="epp-ar epp-s1">1997-02-26</td>
<td role="cell">Lake Stephenborough</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Daisha</td>
<td role="cell">Lind</td>
<td data-value="366508800000" role="cell" class="epp-ar epp-s1">1981-08-13</td>
<td role="cell">Port Mozellshire</td>
</tr>
<tr role="row" scope="row">
<td role="cell">England</td>
<td role="cell">Lavada</td>
<td role="cell">Wiza</td>
<td data-value="-150163200000" role="cell" class="epp-ar epp-s1">1965-03-30</td>
<td role="cell">Gabriellamouth</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Sibyl</td>
<td role="cell">Feest</td>
<td data-value="-934502400000" role="cell" class="epp-ar epp-s1">1940-05-22</td>
<td role="cell">Port Betsybury</td>
</tr>
<tr role="row" scope="row">
<td role="cell">Scotland</td>
<td role="cell">Devante</td>
<td role="cell">Mills</td>
<td data-value="438393600000" role="cell" class="epp-ar epp-s1">1983-11-23</td>
<td role="cell">Lake Katharinafurt</td>
</tr>
</tbody>
</table>
EPPlus converts the table styling in Excel to a separate stylesheet.
table.epplus-table{
font-family:Aptos Narrow;
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;
}
.epp-s1{
white-space: nowrap;
vertical-align:bottom;
}