JSNinja
JSNinja

Reputation: 715

Superscript in excel sheet while exporting using javascript

I'm exporting a table from a HTML page using the following Javascript code:

<script>
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]--></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>

<body>
<table id="maintable">
some table content
</table>
<input type="button" onClick=tableToExcel(maintable,"tablename") />
</body>

This code converts the table to an excel sheet.

But superscript characters are not moved up but stay as it is.

Example: X2 becomes X2 in excel.

I have tried unicode \u00B2 as well but it doesn't work.

Is it possible to export superscript characters to excel? If yes, how can i achieve this.

Thanks in advance.

[Edit] I get output like X² if I use unicode ² or ² or ²

Upvotes: 2

Views: 624

Answers (1)

JSNinja
JSNinja

Reputation: 715

It turns out that the above script is unable to convert unicode properly. The best solution that worked for me is using the html tag sup as follows:

<tr>
    <td>X<sup>2</sup></td>
</tr>

Hope this helps.

Upvotes: 2

Related Questions