Export html content to excel in javascript
14,994
you just add this:
<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>
in the head of the document it will start working as expected:
<script type="text/javascript">
var tableToExcel = (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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
Author by
raviteja
Updated on June 27, 2022Comments
-
raviteja almost 2 years
I am exporting Html content to Excel and download the Excel in JavaScript. It works fine but my requirement is to download Excel without Grid Lines and also I want to render the content as my html shows in browser. Is there any options or suggestions to render Html content in Excel without gridlines?
Below is my sample Html:
<div> <div style="float:left"> NAME:some text </div> <div style="float:right"> Number:some number </div> </div> <div> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> </div>
I don't want those grid cells and my content is not rendered as Html content.
Is there any way to prepare excel with Html data without gridlines in Javascript?
-
raviteja over 7 yearsHey thanks for the reply, its working great than before, but cell A11 need to extend to cell C11 as the data in cell C is last. if any data occupy cell D then Cell A need to ocuppy till cell D. basically need to look like a document in excel is there any way to apply css internal or external? .
-
raviteja over 7 yearsHey thank for your reply, how to include Apache POI in my webApplication, is there any js file ?
-
Anurag Sinha over 7 yearsNope, there is no JS file. It's a java based library. This needs to be done on the server side or via applet(which is almost dead)