本文介绍了在javascript中阅读Excel可以将长整数转换为指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SheetJs读取excel文件,但是问题是它正在将像3577888990098这样的长数字转换成像3.52E+12这样的指数.

I am reading an excel file using SheetJs, but the issue is it's converting long numbers like 3577888990098 to exponential like 3.52E+12.

这个问题不是重复的,因为:

This question is not duplicate, because:

文件列可以是随机的,系统不会知道哪些是数字,哪些是字符串(字母),或者两者都不是.

File columns can be random and system won't know which ones are numbers and which ones are Strings (Alphabetical) or both.

那怎么摆脱呢?

这是我的代码:

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);
    }
}

推荐答案

我通过以下方式解决了此问题:

I have resolved this by:

//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函数可以检查指数是否为指数.

As there is no JS function to check if value if Exponential.

这篇关于在javascript中阅读Excel可以将长整数转换为指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:35