我正在使用SheetJs阅读excel文件,但问题是它将3577888990098
之类的长数字转换为像3.52E+12
这样的指数。
这个问题不重复,因为:
文件列可以是随机的,系统不知道哪些是数字,哪些是字符串(字母)或两者。
那么如何摆脱这个?
这是我的代码:
function handleFileSelect(evt) {
var files = evt.target.files;
var i, f;
//Loop through files
for (i = 0, f = files[i]; i != files.length; ++i) {
var name = f.name;
const reader = new FileReader();
reader.onload = (evt) => {
/* Parse data */
const bstr = evt.target.result;
const wb = XLSX.read(bstr, {type:'binary'});
/* Get first worksheet */
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Convert array of arrays */
const data = XLSX.utils.sheet_to_csv(ws, {header:1});
/* Update state */
console.log("DATA>>>"+data);
};
reader.readAsBinaryString(f);
}
}
答案 0 :(得分:1)
如何仅对Number对象使用toFixed()
var x = 3577888990098;
var y = '3577888990098';
if(typeof(x) === 'number') {
x = x.toFixed(); // it will not convert it to exponential.
}
答案 1 :(得分:0)
我通过以下方式解决了这个问题:
//Assuming there will not be any "+" or "E+" in any values of sheet.
if(cell_data[cell_count].indexOf('E+') !== -1 || cell_data[cell_count].indexOf('+') !== -1) {
console.log(cell_data[cell_count] +" IS EXPONENTIAL");
fixedNumber = Number(cell_data[cell_count]) // it will not convert exponential to number.
onerow.push(fixedNumber);
}else{
onerow.push(cell_data[cell_count]);
}
由于没有JS函数来检查值是否为指数。
答案 2 :(得分:0)
sheet_to_csv
采用格式化的数字。- 为了避免格式化值并获取原始输入(原始值),您必须在 sheet_to_csv
方法中添加参数,您必须将 rawNumbers
设置为 true
强>
试试这个代码
const data = XLSX.utils.sheet_to_csv(worksheet, { rawNumbers: true });