美文网首页
dropzone解析excel文件

dropzone解析excel文件

作者: 抽刀断水2 | 来源:发表于2018-04-24 16:36 被阅读0次
    import XLSX from 'xlsx'
    
    dropzoneOptions: {
                url: 'dummy',
                thumbnailWidth: 50,
                maxFilesize: 50, //MB
                autoProcessQueue: false,
                headers: { "My-Awesome-Header": "header value" },
                accept: function(file, done) {
                    // console.log("file:" + JSON.stringify(file));                
                    var rABS = true
                    var reader = new FileReader();
                    reader.addEventListener("loadend", function(event) {
                        // console.log("event.target.result:" + event.target.result);
                        var data = event.target.result;
                        if(!rABS) data = new Uint8Array(data);
                        var workbook = XLSX.read(data, {type: rABS ? 'binary' : 'array', cellDates:true,
                            dateNF:'yyyy-mm-dd'});
                        // console.log("workbook:" + JSON.stringify(workbook));
                        let worksheet = workbook.Sheets[workbook.SheetNames[0]]
                        var rows = XLSX.utils.sheet_to_row_object_array(worksheet);
                        let oneFileChartDataArr = []
                        for(let i = 0; i < rows.length; i++) {
                            let oneRow = rows[i];
                            let keyArr = Object.keys(oneRow);
                            let date =  oneRow[keyArr[0]];
                            let nav =  oneRow[keyArr[1]];
                            console.log("###date:" + date + " nav:" + nav)
                            oneFileChartDataArr.push({
                                "x": date,
                                "y": nav
                            })
                        }
                        console.log("oneFileChartDataArr:" + JSON.stringify(oneFileChartDataArr));
                    });
                    if(rABS) reader.readAsBinaryString(file); else reader.readAsArrayBuffer(file);
                    // reader.readAsText(file);
                    // reader.readAsBinaryString(file);
                }
            }
    

    相关文章

      网友评论

          本文标题:dropzone解析excel文件

          本文链接:https://www.haomeiwen.com/subject/htvjlftx.html