JavaScript parsing Excel files

Source: Internet
Author: User

Recently done background projects encountered the need to parse the user uploaded Excel file, and export the data in the file needs; In the process of doing this, I felt that my big JavaScript is omnipotent, can read the content in the Excel file by binary way, The data in the Excel file is then read and manipulated further, and the data is directed in the format you want.

Parse read Excel file, there is a very good plug-in, here strongly recommend to everyone:

Spreadsheet Parser and Writer

This plugin can help you to read the data in the Excel file and export the data in JSON format, which is very useful and convenient.

Here is the demo I made during the process of using this plugin:

  

varExcel =function () {         This. data = []//Excel Data         This. Checkresult =false   //Excel specification Test results}excel.prototype.check=function(FILESELECTOR,CHECKSTR,URL,SUC) {varSelf = This; Document.queryselector (Fileselector). AddEventListener (' Change ',function(e) {varFiles =E.target.files; varFileReader =NewFileReader (); //open a file in binary modeFilereader.readasbinarystring (files[0]); Filereader.onload=function(EV) {Try {                    varresult =Ev.target.result, Workbook= Xlsx.read (result, {type: "binary"});//read the entire Excel table object in binary stream mode}Catch(e) {console.error ("File type is incorrect"); return; }                varFromTo = ";//to read the table range of Excel a1:c4                varExcelheadstr = ";//read the Excel header                varHeadcol = ";//table header column number                vararr = [' A1 ', ' B1 ', ' C1 ', ' D1 ', ' E1 ', ' F1 ', ' G1 ', ' H1 ', ' I1 ', ' J1 ', ' K1 ', ' L1 ', ' M1 ', ' N1 ', ' O1 ', ' P1 ', ' Q1 ', ' R1 ', ' S1 ', ' T1 ', ' U1 ', ' V1 ', ' W1 ', ' X1 ', ' Y1 ', ' Z1 ']                varindex; //traverse the Sheets object under workbook to read the contents of each uploaded file                 for(varSheetinchworkbook. Sheets) {if(workbook. Sheets.hasownproperty (sheet)) {FromTo= Workbook. sheets[sheet]["!ref"];//gets the line number and column number of the data in the Excel file                         for(varj = 0; J < 3; J + +) {Excelheadstr+ = workbook. Sheets[sheet][arr[j]].h + ', '                        }                        //remove end of string ', 'Excelheadstr = excelheadstr.substring (0, Excelheadstr.length-1)                        if(Excelheadstr = = =checkstr) {Self.checkresult=truefileUpload (FILESELECTOR,URL,SUC)}Else{console.error (' Excel does not conform to template specification ') Self.checkresult=false} self.data=Self.data.concat (XLSX.utils.sheet_to_json (workbook.                    Sheets[sheet]));        }                }            }; })    }functionFileUpload (fileselector,url,suc) {varFile = Document.queryselector (fileselector). files[0], FormData=NewFormData (), Reader=NewFileReader (); //determine the type of upload file    if(!/^.*?\. (XLS|XLSX) $/i.test (File.name)) {Console.error (' The uploaded file type does not match ')        return false} formdata.append (' File ', file)varXHR =NewXMLHttpRequest (); Xhr.open (' POST ', url,true); Xhr.onreadystatechange=function() {        if(Xhr.readystate = = =xmlhttprequest.done) {vardata =Json.parse (Xhr.responsetext); varsrc =Data.url;        SUC (SRC); }} xhr.onload=function(e) {if(Xhr.status = = 200) {Console.log (' uploaded '); } Else{Console.log (' File upload failed ');    }    }; Xhr.send (FormData);}

  

Call the Excel construction method to parse:

var New Excel ()     a.check (' #excel-file ', ' classification, product name, article number ', ' http://11.com/resource/_upload ',function  (SRC) {        Console.log (src)
Console.log (A.data)
Console.log (A.checkresult)
})

JavaScript parsing Excel files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.