admin管理员组文章数量:1426804
I am doing
XLSX.utils.table_to_book(document.getElementById(table), { sheet: "Sheet JS", raw: false });
Table as an HTML Table. However, one of my columns contains USD ($4.56) but is being read as General type in excel and not as currency. How can i use XLSX.utils.table_to_book and keep certain columns as currency?
I am doing
XLSX.utils.table_to_book(document.getElementById(table), { sheet: "Sheet JS", raw: false });
Table as an HTML Table. However, one of my columns contains USD ($4.56) but is being read as General type in excel and not as currency. How can i use XLSX.utils.table_to_book and keep certain columns as currency?
Share Improve this question asked Oct 31, 2018 at 14:36 Steve ShortSteve Short 951 gold badge1 silver badge13 bronze badges1 Answer
Reset to default 2To change a cell's number format, set the z property of the cell to the format you want to use. For example:
ws["A2"].z = "$0.00";
The formatted text is generally stored in the w
field as opposed to the v
field but isn't automatically updated when you change the number format. You can do it manually:
delete ws["A2"].w; // delete old formatted text if it exists
XLSX.utils.format_cell(ws["A2"]); // refresh cell
Example in the snippet
var tbl = document.getElementById('sheetjs');
var wb = XLSX.utils.table_to_book(tbl);
var ws = wb.Sheets["Sheet1"]; // get the current sheet
console.log(ws["A2"].v); // default v value '4.56'
ws["A2"].z = "$0.00"; // format the cell
delete ws["A2"].w; // delete old formatted text if it exists
XLSX.utils.format_cell(ws["A2"]); // refresh cell
console.log(ws["A2"].w); // new formatted cell '$4.56'
<script src="https://cdnjs.cloudflare./ajax/libs/xlsx/0.14.0/xlsx.js"></script>
<table id="sheetjs">
<tr><td>S</td><td>h</td><td>e</td><td>e</td><td>t</td><td>J</td><td>S</td></tr>
<tr><td>$4.56</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
</table>
本文标签: javascriptXLSXutilstabletobook Currency Coming As GeneralStack Overflow
版权声明:本文标题:javascript - XLSX.utils.table_to_book Currency Coming As General - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745450134a2658844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论