如何在 XLSX JS 中不乘以附加 (%) 百分号
How to append (%) percentage sign without multiplying in XLSX JS
在 XLSX 中,无法附加带有 % 符号的单元格值。在尝试时,它会将单元格值乘以 100。
输入:["球", 1000, 201.72].
XLSX 中的预期输出:["Ball", 1000, 201.72%]
当前输出:["球", 1000, 20172.00%]
function test(){
var itemArr = [
["Ball", 1000, 201.72],
["Bell", 800, 0.7],
["Bun", 1500, 1.7612]
];
var arrayOfArray = [];
for (var i = 0; i < itemArr.length; i++) {
var row = [];
for (var j = 0; j < itemArr[i].length; j++) {
var obj = {};
obj.v = itemArr[i][j];
if (j > 0) obj.t = "n";
else obj.t = "s";
if (j == 2) obj.z = "#,##0.00%";
else if (j == 1) obj.z = "#,##0.00";
row.push(obj);
}
arrayOfArray.push(row);
}
var ws = XLSX.utils.aoa_to_sheet(arrayOfArray);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
return XLSX.writeFile(wb, "test.xlsx");
}
从格式中删除“%”,然后将其附加到值中。以下是相关更改:
if (j == 2) {
obj.z = "#,##0.00";
obj.v += '%';
}
更新代码:
document.getElementById("test").addEventListener("click", function () {
var itemArr = [
["Ball", 1000, 201.72],
["Bell", 800, 0.7],
["Bun", 1500, 1.7612]
];
var arrayOfArray = [];
for (var i = 0; i < itemArr.length; i++) {
var row = [];
for (var j = 0; j < itemArr[i].length; j++) {
var obj = {};
obj.v = itemArr[i][j];
if (j > 0) obj.t = "n";
else obj.t = "s";
if (j == 2) {
obj.z = "#,##0.00"; // No percent sign here
obj.v += '%'; // Append it afterwards
}
else if (j == 1) obj.z = "#,##0.00";
row.push(obj);
}
arrayOfArray.push(row);
}
var ws = XLSX.utils.aoa_to_sheet(arrayOfArray);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
return XLSX.writeFile(wb, "test.xlsx");
});
它使用类似于 Excel 的数字格式化程序,因此无论何时您使用百分号,它都会自动乘以 100。
通过 SheetJS 文档
The rules are slightly different from how Excel displays custom number formats. In particular, literal characters must be wrapped in double quotes or preceded by a backslash. For more info, see the Excel documentation article Create or delete a custom number format or ECMA-376 18.8.31 (Number Formats)
您可以通过在其周围添加引号来添加百分比文字
if (j == 2) obj.z = '#,#0.00"%"';
在 XLSX 中,无法附加带有 % 符号的单元格值。在尝试时,它会将单元格值乘以 100。
输入:["球", 1000, 201.72].
XLSX 中的预期输出:["Ball", 1000, 201.72%]
当前输出:["球", 1000, 20172.00%]
function test(){
var itemArr = [
["Ball", 1000, 201.72],
["Bell", 800, 0.7],
["Bun", 1500, 1.7612]
];
var arrayOfArray = [];
for (var i = 0; i < itemArr.length; i++) {
var row = [];
for (var j = 0; j < itemArr[i].length; j++) {
var obj = {};
obj.v = itemArr[i][j];
if (j > 0) obj.t = "n";
else obj.t = "s";
if (j == 2) obj.z = "#,##0.00%";
else if (j == 1) obj.z = "#,##0.00";
row.push(obj);
}
arrayOfArray.push(row);
}
var ws = XLSX.utils.aoa_to_sheet(arrayOfArray);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
return XLSX.writeFile(wb, "test.xlsx");
}
从格式中删除“%”,然后将其附加到值中。以下是相关更改:
if (j == 2) {
obj.z = "#,##0.00";
obj.v += '%';
}
更新代码:
document.getElementById("test").addEventListener("click", function () {
var itemArr = [
["Ball", 1000, 201.72],
["Bell", 800, 0.7],
["Bun", 1500, 1.7612]
];
var arrayOfArray = [];
for (var i = 0; i < itemArr.length; i++) {
var row = [];
for (var j = 0; j < itemArr[i].length; j++) {
var obj = {};
obj.v = itemArr[i][j];
if (j > 0) obj.t = "n";
else obj.t = "s";
if (j == 2) {
obj.z = "#,##0.00"; // No percent sign here
obj.v += '%'; // Append it afterwards
}
else if (j == 1) obj.z = "#,##0.00";
row.push(obj);
}
arrayOfArray.push(row);
}
var ws = XLSX.utils.aoa_to_sheet(arrayOfArray);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
return XLSX.writeFile(wb, "test.xlsx");
});
它使用类似于 Excel 的数字格式化程序,因此无论何时您使用百分号,它都会自动乘以 100。
通过 SheetJS 文档
The rules are slightly different from how Excel displays custom number formats. In particular, literal characters must be wrapped in double quotes or preceded by a backslash. For more info, see the Excel documentation article Create or delete a custom number format or ECMA-376 18.8.31 (Number Formats)
您可以通过在其周围添加引号来添加百分比文字
if (j == 2) obj.z = '#,#0.00"%"';