Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

CodeProjectJavaScriptJQuery
Home›CodeProject›Export Hierarchical (Multi-Level) HTML Table With Styles Using jQuery

Export Hierarchical (Multi-Level) HTML Table With Styles Using jQuery

By SibeeshVenu
August 13, 2015
3741
0
Share:
Export Hierarchical HTML Table

Introduction

Hi. We are all familiar with HTML tables. What if we want to export that HTML table to Excel? What if we want to do it in the client side itself? Yeah, of course using jQuery. Please read here for the basic exporting technique: Export From HTML Table Using jQuery.

Background

In the preceding article you can determine whether we are exporting a single-level HTML table. What if we need to do that for a multi-level HTML table? The hierarchy must be applied to the Excel file we exported. That too is without using any third-party plug- ins! Sounds cool, right?

Using the code

So shall we start? I hope you said yes.

What we need first

Yeah you are right, we need a multi-level HTML table that we will now export. Let us say I have an HTML table as follows.
[html]
<table id=“multiLevelTable”>
<caption>My Multi Level Table</caption>
<thead>
<tr>
<th colspan=“2” rowspan=“2”>Column 0</th>
<th rowspan=“2”>Column 1</th>
<th colspan=“2”>Column 2</th>
</tr>
<tr>
<th>Column 2a</th>
<th>Column 2b</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan=“2”>Row 1</th>
<th>Row 1a</th>
<td>123</td>
<td>456</td>
<td>789</td>
</tr>
<tr>
<th>Row 1b</th>
<td>123</td>
<td>456</td>
<td>789</td>
</tr>
<tr>
<th colspan=“2”>Row 2</th>
<td>123</td>
<td>456</td>
<td>789</td>
</tr>
</tbody>
</table>
[/html]

What next?

Apply some styles

Please add the following styles.
[css]
<style>
td,th,thead,caption,tr{
text-align:center;border:1px solid #ccc;
}
caption {
background-color:#ccc;
}
[/css]

Is that done?

Add the jQuery reference
[js]
<script src=“Contents/jquery-1.9.1.js”></script>
[/js]

Create an element on which we need to fire the click event.
[html]
<div onclick=“exportThis()” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”>Export Multi Level Table to Excel</div>
[/html]

Please note that we have called the function exportThis in onclick. Shall we go and see what we can write in that function?

exportThis function

The following is the code inside in the function.
[js]
var exportThis = (function () {
var uri = ‘data:application/vnd.ms-excel;base64,’,
template = ‘<html xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:x=”urn:schemas-microsoft-com:office:excel” xmlns=”http://www.w3.org/TR/REC-html40″><head> <!–[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets> <x:ExcelWorksheet><x:Name>{worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook> </xml><![endif]–></head><body> <table>{table}</table></body></html>’,
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
}
return function () {
var ctx = { worksheet: ‘Multi Level Export Table Example’ || ‘Worksheet’, table: document.getElementById(“multiLevelTable”).innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
[/js]

In the preceding specified function, you can see the following three parts:

  • Uri
  • base64
  • format
  • template
  • So we will discuss those terms now.

    If you are confused about the terms URI and URL please read here: What is the difference between URI, URL and URN?

    To be familiar with the base 64 encoding please read: What is base 64 encoding used for?

    The format part is cross-checking the data, whether it has some invalid characters or not. You can see a regex in the function.

    The template is the structure of the Excel file; we are using the w3 format.

    What it does

    Once we clicked the div, the onclick event will be fired and it fetches the inner HTML of the element multiLevelTable. Once it does, we are formulating the data and assigning it to the location.href().

    Please see here to understand the basic difference between the window.location.href() and window.open() : Window.location.href () and Window.open () methods in JavaScript.

    Wow cool, we did it.

    Export Hierarchical HTML Table

    Export Hierarchical HTML Table

    Figure: without style.

    Export Hierarchical HTML Table

    Export Hierarchical HTML Table

    Figure: with style

    Wait, we are not done yet

    Now what if you have many HTML tables and you may need to export all the HTML tables? Of course we cannot create separate functions, right?

    So can we do some changes to our function?

    Let us do this.

    New export function
    [js]
    var exportThisWithParameter = (function () {
    var uri = ‘data:application/vnd.ms-excel;base64,’, template = ‘<html xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:x=”urn:schemas-microsoft-com:office:excel” xmlns=”http://www.w3.org/TR/REC-html40″><head> <!–[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets> <x:ExcelWorksheet><x:Name>{worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook> </xml><![endif]–></head><body> <table>{table}</table></body></html>’,
    base64 = function (s) {
    return window.btoa(unescape(encodeURIComponent(s)))
    },
    format = function (s, c) {
    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
    }
    return function (tableID, excelName) {
    tableID = document.getElementById(tableID)
    var ctx = { worksheet: excelName || ‘Worksheet’, table: tableID.innerHTML }
    window.location.href = uri + base64(format(template, ctx))
    }
    })()
    [/js]

    In the preceding function you can see the parameters (tableID, excelName) . This is what we need to pass from the click event as follows.
    [html]
    <div onclick=“exportThisWithParameter(‘multiLevelTable’, ‘Multi Level Export Table Example’)” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”>Export Multi Level Table to Excel With Parameter</div>
    [/html]

    Complete HTML
    [html]
    <!DOCTYPE html>
    <html xmlns=“http://www.w3.org/1999/xhtml”>
    <head>
    <title>Export Hierarchical HTML Using JQuery – Sibeesh|Passion</title>
    <script src=“Contents/jquery-1.9.1.js”></script>
    <style>
    td,th,thead,caption,tr{
    text-align:center;border:1px solid #ccc;
    }
    caption {
    background-color:#ccc;
    }
    </style>
    <script type=“text/javascript”>
    var exportThis = (function () {
    var uri = ‘data:application/vnd.ms-excel;base64,’,
    template = ‘<html xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:x=”urn:schemas-microsoft-com:office:excel” xmlns=”http://www.w3.org/TR/REC-html40″><head> <!–[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets> <x:ExcelWorksheet><x:Name>{worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook> </xml><![endif]–></head><body> <table>{table}</table></body></html>’,
    base64 = function (s) {
    return window.btoa(unescape(encodeURIComponent(s)))
    },
    format = function (s, c) {
    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
    }
    return function () {
    var ctx = { worksheet: ‘Multi Level Export Table Example’ || ‘Worksheet’, table: document.getElementById(“multiLevelTable”).innerHTML }
    window.location.href = uri + base64(format(template, ctx))
    }
    })()
    var exportThisWithParameter = (function () {
    var uri = ‘data:application/vnd.ms-excel;base64,’,
    template = ‘<html xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:x=”urn:schemas-microsoft-com:office:excel” xmlns=”http://www.w3.org/TR/REC-html40″><head> <!–[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets> <x:ExcelWorksheet><x:Name>{worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook> </xml><![endif]–></head><body> <table>{table}</table></body></html>’,
    base64 = function (s) {
    return window.btoa(unescape(encodeURIComponent(s)))
    },
    format = function (s, c) {
    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
    }
    return function (tableID, excelName) {
    tableID = document.getElementById(tableID)
    var ctx = { worksheet: excelName || ‘Worksheet’, table: tableID.innerHTML }
    window.location.href = uri + base64(format(template, ctx))
    }
    })()
    </script>
    </head>
    <body>
    <div onclick=“exportThis()” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”>Export Multi Level Table to Excel</div>
    <br />
    <div onclick=“exportThisWithParameter(‘multiLevelTable’, ‘Multi Level Export Table Example’)” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”>Export Multi Level Table to Excel With Parameter</div>
    <br />
    <br />
    <table id=“multiLevelTable”>
    <caption>My Multi Level Table</caption>
    <thead>
    <tr>
    <th colspan=“2” rowspan=“2” style=“background-color:aqua;”>Column 0</th>
    <th rowspan=“2”>Column 1</th>
    <th colspan=“2”>Column 2</th>
    </tr>
    <tr>
    <th>Column 2a</th>
    <th>Column 2b</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <th rowspan=“2”>Row 1</th>
    <th>Row 1a</th>
    <td>123</td>
    <td>456</td>
    <td>789</td>
    </tr>
    <tr>
    <th>Row 1b</th>
    <td>123</td>
    <td>456</td>
    <td>789</td>
    </tr>
    <tr>
    <th colspan=“2”>Row 2</th>
    <td>123</td>
    <td>456</td>
    <td>789</td>
    </tr>
    </tbody>
    </table>
    </body>
    </html>
    [/html]

    Points of interests

    Hierarchical HTML, HTML, JQuery, Multi Level HTML

    History

    First Version : 19-Jan-2015

    Conclusion

    That is all for the day. I hope you enjoyed this article. Thanks for reading. Please provide your valuable suggestions.

    Kindest Regards
    Sibeesh Venu

    TagsExport HTMLExport multi level tableExport tableHierarchical HTMLHTMLJQueryMulti Level HTML
    Previous Article

    Working With Charts Using Spire.XLS

    Next Article

    Make Image Fit To The Screen

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • JQuery

      How to disable F12 Key in a page using JQuery

      July 16, 2015
      By SibeeshVenu
    • JQuery

      Move Elements on Mouse Click Using jQuery

      May 31, 2015
      By SibeeshVenu
    • Line Chart With Custom Tool Tip
      How toHTML5

      Client Side Chart Widget in HTML 5: Part 7 (Line Chart With Custom ToolTip)

      January 29, 2015
      By SibeeshVenu
    • JQueryJson

      Find JSON Objects With Same Property and Separate Them

      May 29, 2015
      By SibeeshVenu
    • Code SnippetsJQuery

      How to add a css class to an element in JQuery

      May 31, 2015
      By SibeeshVenu
    • HTML5

      Client-Side Chart Widget in HTML5: Part 4 (Doughnut Chart)

      January 29, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version