<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Export HTML &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/export-html/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 27 Feb 2018 06:07:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Export HTML &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Export Hierarchical (Multi-Level) HTML Table With Styles Using jQuery</title>
		<link>https://www.sibeeshpassion.com/export-hierarchical-multi-level-html-table-with-styles-using-jquery/</link>
					<comments>https://www.sibeeshpassion.com/export-hierarchical-multi-level-html-table-with-styles-using-jquery/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 13 Aug 2015 00:13:42 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Export HTML]]></category>
		<category><![CDATA[Export multi level table]]></category>
		<category><![CDATA[Export table]]></category>
		<category><![CDATA[Hierarchical HTML]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Multi Level HTML]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=501</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><span style="color: #ff6600;">Introduction</span></p>
<p>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.</p>
<p><span style="color: #ff6600;">Background</span></p>
<p>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?</p>
<p><span style="color: #ff6600;">Using the code</span></p>
<p>So shall we start? I hope you said yes.</p>
<p><span style="color: #ff6600;">What we need first</span></p>
<p>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.<br />
[html]<br />
&lt;table id=“multiLevelTable”&gt;<br />
        &lt;caption&gt;My Multi Level Table&lt;/caption&gt;<br />
        &lt;thead&gt;<br />
            &lt;tr&gt;<br />
                &lt;th colspan=“2” rowspan=“2”&gt;Column 0&lt;/th&gt;<br />
                &lt;th rowspan=“2”&gt;Column 1&lt;/th&gt;<br />
                &lt;th colspan=“2”&gt;Column 2&lt;/th&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th&gt;Column 2a&lt;/th&gt;<br />
                &lt;th&gt;Column 2b&lt;/th&gt;<br />
            &lt;/tr&gt;<br />
        &lt;/thead&gt;<br />
        &lt;tbody&gt;<br />
            &lt;tr&gt;<br />
                &lt;th rowspan=“2”&gt;Row 1&lt;/th&gt;<br />
                &lt;th&gt;Row 1a&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th&gt;Row 1b&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th colspan=“2”&gt;Row 2&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
        &lt;/tbody&gt;<br />
    &lt;/table&gt;<br />
[/html]</p>
<p>What next?</p>
<p><span style="color: #ff6600;">Apply some styles</span></p>
<p>Please add the following styles.<br />
[css]<br />
&lt;style&gt;<br />
    td,th,thead,caption,tr{<br />
        text-align:center;border:1px solid #ccc;<br />
    }<br />
        caption {<br />
            background-color:#ccc;<br />
        }<br />
[/css]</p>
<p>Is that done?</p>
<p><span style="color: #ff6600;">Add the jQuery reference</span><br />
[js]<br />
&lt;script src=“Contents/jquery-1.9.1.js”&gt;&lt;/script&gt;<br />
[/js]</p>
<p><span style="color: #ff6600;">Create an element on which we need to fire the click event.<br />
[html]<br />
&lt;div onclick=“exportThis()” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”&gt;Export Multi Level Table to Excel&lt;/div&gt;<br />
[/html]</p>
<p>Please note that we have called the function exportThis in onclick. Shall we go and see what we can write in that function?</p>
<p><span style="color: #ff6600;">exportThis function</span></p>
<p>The following is the code inside in the function.<br />
[js]<br />
var exportThis = (function () {<br />
            var uri = ‘data:application/vnd.ms-excel;base64,’,<br />
template = ‘&lt;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″&gt;&lt;head&gt; &lt;!–[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt; &lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt; &lt;x:WorksheetOptions&gt;&lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt; &lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt; &lt;/xml&gt;&lt;![endif]–&gt;&lt;/head&gt;&lt;body&gt; &lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;’,<br />
                base64 = function (s) {<br />
                    return window.btoa(unescape(encodeURIComponent(s)))<br />
                },<br />
                format = function (s, c) {<br />
                    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })<br />
                }<br />
            return function () {<br />
                var ctx = { worksheet: ‘Multi Level Export Table Example’ || ‘Worksheet’, table: document.getElementById(“multiLevelTable”).innerHTML }<br />
                window.location.href = uri + base64(format(template, ctx))<br />
            }<br />
        })()<br />
[/js]</p>
<p>In the preceding specified function, you can see the following three parts:</p>
<li>Uri</li>
<li>base64</li>
<li>format</li>
<li>template</li>
<p>So we will discuss those terms now.</p>
<p>If you are confused about the terms URI and URL please read here: <a href="http://stackoverflow.com/questions/4913343/what-is-the-difference-between-uri-url-and-urn" target="_blank">What is the difference between URI, URL and URN?</a></p>
<p>To be familiar with the base 64 encoding please read: <a href="http://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for" target="_blank">What is base 64 encoding used for?</a></p>
<p>The format part is cross-checking the data, whether it has some invalid characters or not. You can see a regex in the function.</p>
<p>The template is the structure of the Excel file; we are using the w3 format.</p>
<p><span style="color: #ff6600;">What it does</span></p>
<p>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().</p>
<p>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.</p>
<p>Wow cool, we did it.</p>
<div id="attachment_9491" style="width: 371px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table1.jpg"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-9491" src="http://sibeeshpassion.com/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table1.jpg" alt="Export Hierarchical HTML Table" width="361" height="154" class="size-full wp-image-9491" srcset="/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table1.jpg 361w, /wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table1-300x128.jpg 300w" sizes="(max-width: 361px) 100vw, 361px" /></a><p id="caption-attachment-9491" class="wp-caption-text">Export Hierarchical HTML Table</p></div>
<p>Figure: without style.</p>
<div id="attachment_9492" style="width: 364px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table2.jpg"><img decoding="async" aria-describedby="caption-attachment-9492" src="http://sibeeshpassion.com/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table2.jpg" alt="Export Hierarchical HTML Table" width="354" height="144" class="size-full wp-image-9492" srcset="/wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table2.jpg 354w, /wp-content/uploads/2014/11/Export-Hierarchical-HTML-Table2-300x122.jpg 300w" sizes="(max-width: 354px) 100vw, 354px" /></a><p id="caption-attachment-9492" class="wp-caption-text">Export Hierarchical HTML Table</p></div>
<p>Figure: with style</p>
<p>Wait, we are not done yet</p>
<p>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?</p>
<p>So can we do some changes to our function?</p>
<p>Let us do this.</p>
<p><span style="color: #ff6600;">New export function</span><br />
[js]<br />
var exportThisWithParameter = (function () {<br />
            var uri = ‘data:application/vnd.ms-excel;base64,’, template = ‘&lt;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″&gt;&lt;head&gt; &lt;!–[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt; &lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt; &lt;x:WorksheetOptions&gt;&lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt; &lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt; &lt;/xml&gt;&lt;![endif]–&gt;&lt;/head&gt;&lt;body&gt; &lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;’,<br />
                base64 = function (s) {<br />
                    return window.btoa(unescape(encodeURIComponent(s)))<br />
                },<br />
                format = function (s, c) {<br />
                    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })<br />
                }<br />
            return function (tableID, excelName) {<br />
                tableID = document.getElementById(tableID)<br />
                var ctx = { worksheet: excelName || ‘Worksheet’, table: tableID.innerHTML }<br />
                window.location.href = uri + base64(format(template, ctx))<br />
            }<br />
        })()<br />
[/js]</p>
<p>In the preceding function you can see the parameters (tableID, excelName) . This is what we need to pass from the click event as follows.<br />
[html]<br />
&lt;div onclick=“exportThisWithParameter(‘multiLevelTable’, ‘Multi Level Export Table Example’)” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”&gt;Export Multi Level Table to Excel With Parameter&lt;/div&gt;<br />
[/html]</p>
<p><span style="color: #ff6600;">Complete HTML</span><br />
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html xmlns=“http://www.w3.org/1999/xhtml”&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Export Hierarchical HTML Using JQuery – Sibeesh|Passion&lt;/title&gt;<br />
    &lt;script src=“Contents/jquery-1.9.1.js”&gt;&lt;/script&gt;<br />
    &lt;style&gt;<br />
    td,th,thead,caption,tr{<br />
        text-align:center;border:1px solid #ccc;<br />
    }<br />
        caption {<br />
            background-color:#ccc;<br />
        }<br />
&lt;/style&gt;<br />
    &lt;script type=“text/javascript”&gt;<br />
        var exportThis = (function () {<br />
            var uri = ‘data:application/vnd.ms-excel;base64,’,<br />
                template = ‘&lt;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″&gt;&lt;head&gt; &lt;!–[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt; &lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt; &lt;x:WorksheetOptions&gt;&lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt; &lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt; &lt;/xml&gt;&lt;![endif]–&gt;&lt;/head&gt;&lt;body&gt; &lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;’,<br />
                base64 = function (s) {<br />
                    return window.btoa(unescape(encodeURIComponent(s)))<br />
                },<br />
                format = function (s, c) {<br />
                    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })<br />
                }<br />
            return function () {<br />
                var ctx = { worksheet: ‘Multi Level Export Table Example’ || ‘Worksheet’, table: document.getElementById(“multiLevelTable”).innerHTML }<br />
                window.location.href = uri + base64(format(template, ctx))<br />
            }<br />
        })()<br />
        var exportThisWithParameter = (function () {<br />
            var uri = ‘data:application/vnd.ms-excel;base64,’,<br />
                template = ‘&lt;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″&gt;&lt;head&gt; &lt;!–[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt; &lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt; &lt;x:WorksheetOptions&gt;&lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt; &lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt; &lt;/xml&gt;&lt;![endif]–&gt;&lt;/head&gt;&lt;body&gt; &lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;’,<br />
                base64 = function (s) {<br />
                    return window.btoa(unescape(encodeURIComponent(s)))<br />
                },<br />
                format = function (s, c) {<br />
                    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })<br />
                }<br />
            return function (tableID, excelName) {<br />
                tableID = document.getElementById(tableID)<br />
                var ctx = { worksheet: excelName || ‘Worksheet’, table: tableID.innerHTML }<br />
                window.location.href = uri + base64(format(template, ctx))<br />
            }<br />
        })()<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;div onclick=“exportThis()” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”&gt;Export Multi Level Table to Excel&lt;/div&gt;<br />
    &lt;br /&gt;<br />
    &lt;div onclick=“exportThisWithParameter(‘multiLevelTable’, ‘Multi Level Export Table Example’)” style=“cursor: pointer; border: 1px solid #ccc; text-align: center;width:19%;”&gt;Export Multi Level Table to Excel With Parameter&lt;/div&gt;<br />
    &lt;br /&gt;<br />
    &lt;br /&gt;<br />
    &lt;table id=“multiLevelTable”&gt;<br />
        &lt;caption&gt;My Multi Level Table&lt;/caption&gt;<br />
        &lt;thead&gt;<br />
            &lt;tr&gt;<br />
                &lt;th colspan=“2” rowspan=“2” style=“background-color:aqua;”&gt;Column 0&lt;/th&gt;<br />
                &lt;th rowspan=“2”&gt;Column 1&lt;/th&gt;<br />
                &lt;th colspan=“2”&gt;Column 2&lt;/th&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th&gt;Column 2a&lt;/th&gt;<br />
                &lt;th&gt;Column 2b&lt;/th&gt;<br />
            &lt;/tr&gt;<br />
        &lt;/thead&gt;<br />
        &lt;tbody&gt;<br />
            &lt;tr&gt;<br />
                &lt;th rowspan=“2”&gt;Row 1&lt;/th&gt;<br />
                &lt;th&gt;Row 1a&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th&gt;Row 1b&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
            &lt;tr&gt;<br />
                &lt;th colspan=“2”&gt;Row 2&lt;/th&gt;<br />
                &lt;td&gt;123&lt;/td&gt;<br />
                &lt;td&gt;456&lt;/td&gt;<br />
                &lt;td&gt;789&lt;/td&gt;<br />
            &lt;/tr&gt;<br />
        &lt;/tbody&gt;<br />
    &lt;/table&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><span style="color: #ff6600;">Points of interests</span></p>
<p>Hierarchical HTML, HTML, JQuery, Multi Level HTML</p>
<p><span style="color: #ff6600;">History</span></p>
<p>First Version : 19-Jan-2015</p>
<p><span style="color: #ff6600;">Conclusion</span></p>
<p>That is all for the day. I hope you enjoyed this article. Thanks for reading. Please provide your valuable suggestions.</p>
<p>Kindest Regards<br />
<a href="http://sibeeshpassion.com" target="_blank">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/export-hierarchical-multi-level-html-table-with-styles-using-jquery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Export From HTML Table Using jQuery</title>
		<link>https://www.sibeeshpassion.com/export-html-table-using-jquery/</link>
					<comments>https://www.sibeeshpassion.com/export-html-table-using-jquery/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 29 Oct 2014 18:55:52 +0000</pubDate>
				<category><![CDATA[Exporting]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Export Excel]]></category>
		<category><![CDATA[Export From HTML Table]]></category>
		<category><![CDATA[Export HTML]]></category>
		<category><![CDATA[Export PDF]]></category>
		<category><![CDATA[Export PPT]]></category>
		<category><![CDATA[Export Word]]></category>
		<category><![CDATA[JQuery Export]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=352</guid>

					<description><![CDATA[[toc] Introduction In this article, we will see how to export from an HTML table using jQuery. We all work in some applications where we are playing with data&#8217;s. It might be some data returned by the server or it might be some client-side data like HTML table. No matter whichever forms the data is, there will be an export option. Isn&#8217;t it? 80% of our clients need the data to be exported to excel. So we need to give the option to export the given data to excel, not dependent on the data format. In my case, my data [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>In this article, we will see how to export from an HTML table using jQuery. We all work in some applications where we are playing with data&#8217;s. It might be some data returned by the server or it might be some client-side data like HTML table. No matter whichever forms the data is, there will be an export option. Isn&#8217;t it? 80% of our clients need the data to be exported to excel. So we need to give the option to export the given data to excel, not dependent on the data format. In my case, my data is in the form of an HTML table, so in this post, we will explain how to export an HTML data to excel, pdf, png, jpeg etc. I hope you will like it.</p>
<p>Download Files here: <a href="https://code.msdn.microsoft.com/Export-From-HTML-Table-c479ff99" target="_blank" rel="noopener">HTML Table Export</a></p>
<h2>Background</h2>
<p>As I said earlier, this article explains how to export an HTML table using jQuery. In this article we will see how we can export the given data to the following formats:</p>
<ul>
<li>Excel</li>
<li>PDF</li>
<li>Image</li>
<li>CSV</li>
<li>PPT</li>
<li>Word</li>
<li>TXT</li>
<li>XML</li>
</ul>
<p>You can always see other exporting related articles here.</p>
<ul>
<li><a href="http://sibeeshpassion.com/export-hierarchical-multi-level-html-table-with-styles-using-jquery/" target="_blank" rel="noopener">Export Hierarchical (Multi-Level) HTML Table </a></li>
<li><a href="http://sibeeshpassion.com/compress-xml-string-variables-in-client-side-and-export-in-html5-using-jquery/" target="_blank" rel="noopener">Compress And Export</a></li>
</ul>
<h2><strong>Why</strong></h2>
<p>In my project, we are doing 80% of our work in the client side. So I decided to implement the export feature in the client side itself. The same can be done using server-side code also but I prefer the client-side one.</p>
<h3><strong>What you must know</strong></h3>
<ul>
<li><a href="http://jquery.com/" target="_blank" rel="noopener">jQuery</a></li>
<li><a href="http://www.w3schools.com/js/" target="_blank" rel="noopener">JavaScript</a></li>
<li><a href="http://www.w3schools.com/css/css3_intro.asp" target="_blank" rel="noopener">CSS 3</a></li>
<li><a href="http://www.w3schools.com/html/" target="_blank" rel="noopener">HTML</a></li>
<li><a href="http://www.tutorialspoint.com/jquery/jquery-dom.htm" target="_blank" rel="noopener">DOM Manipulations in jQuery</a></li>
</ul>
<h3><strong>Using the code</strong></h3>
<p>Before you start, you need to download the necessary files from <a href="https://github.com/kayalshri/tableExport.jquery.plugin" target="_blank" rel="noopener">TableExport.jquery.plugin</a>.</p>
<p>Once you have download the files, you need to include those in your project. Here I am using a MVC4 web application.</p>
<p>[js]<br />
&lt;script src=“~/Contents/jquery-1.9.1.js”&gt;&lt;/script&gt;<br />
&lt;script src=“~/Contents/tableExport.js”&gt;&lt;/script&gt; @*Main file which does export feature *@<br />
&lt;script src=“~/Contents/jquery.base64.js”&gt;&lt;/script&gt; @*Main file which does convert the content to base64 *@<br />
&lt;script src=“~/Contents/html2canvas.js”&gt;&lt;/script&gt; @*Main file which does export to image feature *@<br />
&lt;script src=“~/Contents/jspdf/libs/base64.js”&gt;&lt;/script&gt; @*Main file which does convert the content to base64 for pdf *@<br />
&lt;script src=“~/Contents/jspdf/libs/sprintf.js”&gt;&lt;/script&gt; @*Main file which does export feature for pdf *@<br />
&lt;script src=“~/Contents/jspdf/jspdf.js”&gt;&lt;/script&gt; @*Main file which does export feature for pdf *@<br />
[/js]</p>
<p>Let’s say you have an HTML table as follows:</p>
<p>[html]<br />
&lt;table id=“activity” &gt;<br />
&lt;thead&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;Name&lt;/th&gt;<br />
&lt;th&gt;Activity on Code Project (%)&lt;/th&gt;<br />
&lt;th&gt;Activity on C# Corner (%)&lt;/th&gt;<br />
&lt;th&gt;Activity on Asp Forum (%)&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;/thead&gt;<br />
&lt;tbody&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Sibeesh&lt;/td&gt;<br />
&lt;td&gt;100&lt;/td&gt;<br />
&lt;td&gt;98&lt;/td&gt;<br />
&lt;td&gt;80&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Ajay&lt;/td&gt;<br />
&lt;td&gt;90&lt;/td&gt;<br />
&lt;td&gt;0&lt;/td&gt;<br />
&lt;td&gt;50&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Ansary&lt;/td&gt;<br />
&lt;td&gt;100&lt;/td&gt;<br />
&lt;td&gt;20&lt;/td&gt;<br />
&lt;td&gt;10&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Aghil&lt;/td&gt;<br />
&lt;td&gt;0&lt;/td&gt;<br />
&lt;td&gt;30&lt;/td&gt;<br />
&lt;td&gt;90&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Arya&lt;/td&gt;<br />
&lt;td&gt;0&lt;/td&gt;<br />
&lt;td&gt;0&lt;/td&gt;<br />
&lt;td&gt;95&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/tbody&gt;<br />
&lt;/table&gt;<br />
[/html]</p>
<p>Add some more UI elements for firing the click events.</p>
<p>[html]<br />
&lt;table&gt;<br />
&lt;tr id=“footerExport”&gt;<br />
&lt;td id=“exportexcel”&gt;&lt;img src=“~/icons/xls.png” title=“Export to Excel” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportpdf”&gt;&lt;img src=“~/icons/pdf.png” title=“Export to PDF” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportimage”&gt;&lt;img src=“~/icons/png.png” title=“Export to PNG” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportcsv”&gt;&lt;img src=“~/icons/csv.png” title=“Export to CSV” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportword”&gt;&lt;img src=“~/icons/word.png” title=“Export to Word” /&gt;&lt;/td&gt;<br />
&lt;td id=“exporttxt”&gt;&lt;img src=“~/icons/txt.png” title=“Export to TXT” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportxml”&gt;&lt;img src=“~/icons/xml.png” title=“Export to XML” /&gt;&lt;/td&gt;<br />
&lt;td id=“exportppt”&gt;&lt;img src=“~/icons/ppt.png” title=“Export to PPT” /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
[/html]</p>
<p>Add a few styles to the table to make it viewable.</p>
<p>[css]<br />
&lt;style&gt;<br />
#activity {<br />
text-align:center;border:1px solid #ccc;<br />
}<br />
#activity td{<br />
text-align:center;border:1px solid #ccc;<br />
}<br />
#footerExport td{<br />
cursor:pointer;<br />
text-align:center;border:1px solid #ccc;<br />
border:none;<br />
}<br />
&lt;/style&gt;<br />
[/css]</p>
<p>Now it is time to fire our events 🙂 You can do that as in the following.<br />
[js]<br />
&lt;script&gt;<br />
$(document).ready(function () {<br />
$(‘#exportexcel’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘excel’, escape: ‘false’ });<br />
});<br />
$(‘#exportpdf’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘pdf’, escape: ‘false’ });<br />
});<br />
$(‘#exportimage’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘png’, escape: ‘false’ });<br />
});<br />
$(‘#exportcsv’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘csv’, escape: ‘false’ });<br />
});<br />
$(‘#exportppt’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘powerpoint’, escape: ‘false’ });<br />
});<br />
$(‘#exportxml’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘xml’, escape: ‘false’ });<br />
});<br />
$(‘#exportword’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘doc’, escape: ‘false’ });<br />
});<br />
$(‘#exporttxt’).bind(‘click’, function (e) {<br />
$(‘#activity’).tableExport({ type: ‘txt’, escape: ‘false’ });<br />
});<br />
});<br />
&lt;/script&gt;<br />
[/js]</p>
<p>Please note that you can export to a few more formats in the same way. You can learn more here:tableExport.jquery.plugin.</p>
<h3><strong>Explanation</strong></h3>
<p>Now its time go deeper into that plugin. In the downloaded files you can see a file called tableExport.js. just open that file, you can see some conditions for specific formats. And the default property for exporting as follows.<br />
[js]<br />
var defaults = {<br />
separator: ‘,’,<br />
ignoreColumn: [],<br />
tableName:‘yourTableName’,<br />
type:‘csv’,<br />
pdfFontSize:7,<br />
pdfLeftMargin:20,<br />
escape:‘true’,<br />
htmlContent:‘false’,<br />
consoleLog:‘false’<br />
};<br />
[/js]</p>
<p>You can change these properties depending on your needs as follows.</p>
<p>[js]<br />
var varpdfFontSize= ‘7’;<br />
$(‘#activity’).tableExport({ type: ‘excel’, escape: ‘false’,pdfFontSize:varpdfFontSize});<br />
[/js]</p>
<p>You can try all the properties listed above like this :).</p>
<p>Now in that file you can see an if else if condition that is satisfied for multiple formats. Let me explain for Excel formatting alone.</p>
<p>[js]<br />
var excel=“&lt;table&gt;”;<br />
// Header<br />
$(el).find(‘thead’).find(‘tr’).each(function() {<br />
excel += “&lt;tr&gt;”;<br />
$(this).filter(‘:visible’).find(‘th’).each(function(index,data) {<br />
if ($(this).css(‘display’) != ‘none’){<br />
if(defaults.ignoreColumn.indexOf(index) == -1){<br />
excel += “&lt;td&gt;” + parseString($(this))+ “&lt;/td&gt;”;<br />
}<br />
}<br />
});<br />
excel += ‘&lt;/tr&gt;’;<br />
});<br />
// Row Vs Column<br />
var rowCount=1;<br />
$(el).find(‘tbody’).find(‘tr’).each(function() {<br />
excel += “&lt;tr&gt;”;<br />
var colCount=0;<br />
$(this).filter(‘:visible’).find(‘td’).each(function(index,data) {<br />
if ($(this).css(‘display’) != ‘none’){<br />
if(defaults.ignoreColumn.indexOf(index) == -1){<br />
excel += “&lt;td&gt;”+parseString($(this))+“&lt;/td&gt;”;<br />
}<br />
}<br />
colCount++;<br />
});<br />
rowCount++;<br />
excel += ‘&lt;/tr&gt;’;<br />
});<br />
excel += ‘&lt;/table&gt;’<br />
if(defaults.consoleLog == ‘true’){<br />
console.log(excel);<br />
}<br />
var excelFile = “&lt;html xmlns:o=’urn:schemas-microsoft-com:office:office’ xmlns:x=’urn:schemas-microsoft-com:office:”+defaults.type+“‘ xmlns=’http://www.w3.org/TR/REC-html40′&gt;”;<br />
excelFile += “&lt;head&gt;”;<br />
excelFile += “&lt;!–[if gte mso 9]&gt;”;<br />
excelFile += “&lt;xml&gt;”;<br />
excelFile += “&lt;x:ExcelWorkbook&gt;”;<br />
excelFile += “&lt;x:ExcelWorksheets&gt;”;<br />
excelFile += “&lt;x:ExcelWorksheet&gt;”;<br />
excelFile += “&lt;x:Name&gt;”;<br />
excelFile += “{worksheet}”;<br />
excelFile += “&lt;/x:Name&gt;”;<br />
excelFile += “&lt;x:WorksheetOptions&gt;”;<br />
excelFile += “&lt;x:DisplayGridlines/&gt;”;<br />
excelFile += “&lt;/x:WorksheetOptions&gt;”;<br />
excelFile += “&lt;/x:ExcelWorksheet&gt;”;<br />
excelFile += “&lt;/x:ExcelWorksheets&gt;”;<br />
excelFile += “&lt;/x:ExcelWorkbook&gt;”;<br />
excelFile += “&lt;/xml&gt;”;<br />
excelFile += “&lt;![endif]–&gt;”;<br />
excelFile += “&lt;/head&gt;”;<br />
excelFile += “&lt;body&gt;”;<br />
excelFile += excel;<br />
excelFile += “&lt;/body&gt;”;<br />
excelFile += “&lt;/html&gt;”;<br />
var base64data = “base64,” + $.base64.encode(excelFile);<br />
window.open(‘data:application/vnd.ms-‘+defaults.type+‘;filename=exportData.doc;’ + base64data);<br />
[/js]</p>
<h3><strong>Procedure</strong></h3>
<ul>
<li>Find the UI element (in this case it is our HTML table).</li>
<li>Loop through the rows of the thread for the header information.</li>
<li>Apply a filter to avoid the UI elements that have a display as none <em>(filter(‘:visible’))</em>.</li>
<li>If the UI is visible, append it to the variable <em>(excel += “</em>” + parseString($(this))+ “”;)</li>
<li>The same procedure is done for the row vs column values also. The only difference is, here we are looping through the tbody instead of thread.</li>
<li>After the data is manipulated, data is formulated in the Excel format.</li>
</ul>
<p>You can learn more in the attachment.</p>
<h2><strong>Output</strong></h2>
<div id="attachment_10926" style="width: 300px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2014/10/export.png"><img decoding="async" aria-describedby="caption-attachment-10926" class="size-full wp-image-10926" src="http://sibeeshpassion.com/wp-content/uploads/2014/10/export.png" alt="Export From HTML Table" width="290" height="100" /></a><p id="caption-attachment-10926" class="wp-caption-text">Export From HTML Table</p></div>
<h2><strong>Points of Interest</strong></h2>
<p>jquery, CSS, HTML, Export</p>
<h2><strong>History</strong></h2>
<p>1st version: 18-11-2014<br />
2nd Version: 24-11-2014</p>
<h2><strong>Changes did</strong></h2>
<ol>
<li>Added Images to UI elements</li>
<li>Added some export feature</li>
</ol>
<h2><strong>Conclusion</strong></h2>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><strong>Your turn. What do you think?</strong></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/export-html-table-using-jquery/feed/</wfw:commentRss>
			<slash:comments>135</slash:comments>
		
		
			</item>
	</channel>
</rss>
