Click here to Skip to main content
15,913,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all experts,

I have some items that start 000 before (ex:001110011577,001110011580,001110015862...).
In html's table, it display correct format (001110011577,001110011580,001110015862...).
but when i export to excel, the item code displays incorrect format(1110011577,1110011580,1110015862...). it means that all 00 in front will cut off automatic by excel.
Here is my source code to export to excel:
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]--></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, filename) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = { worksheet: 'Worksheet', table: table.innerHTML }
            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = filename + '.xls';
            document.getElementById("dlink").click();
        }
    })


Does anybody know how to display my item like what it displays html's table?

Thanks
Posted
Comments
enhzflep 27-Aug-13 6:01am    
Not sure how you'd add it with your code, but don't forget that you can force excel to display leading zeros by adding a ' in front of the number.
E.g click cell A1, type '00000111 - the ' forces the number to be displayed as text. You should get a 'note' on the cell telling you that a number is formatted as text.
soeun tony 6-Sep-13 21:44pm    
Yeap, it is the one way but my client can't accept because they need to cut ' of the item and they take long time to do this.
Do you have other idea?
Thanks

1 solution

tabletoexcel('GrdAccountTypesListView', 'AccountTypesListView', 'AccountTypesListView.xlsx')
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 xmlns:x="#unknown"><x:excelworksheets><x:excelworksheet><x:name>{worksheet}<x:worksheetoptions><x:displaygridlines></xml><![endif]--></head><body>{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, filename) {


if (!table.nodeType) table = document.getElementById(tables)

var ctx = { worksheet: name || 'Worksheet', table: tables.innerHTML }
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();

}

})()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900