如何使用javascript选择或检索excelsheet的单元格数据?

我有 Excel 工作表,我想使用 javascript 选择特定单元格。

http://img.mukewang.com/64a686cd0001f52406520373.jpg

我有总数、年龄、位置、教育程度等过滤器,并且想要过滤其数据,如 01##、02##、03## 等

所以我想选择这个工作表数据。

谢谢你!


不负相思意
浏览 132回答 1
1回答

素胚勾勒不出你

首先laod excel表格数据var csvdata = "-1";if (window.XMLHttpRequest)&nbsp; &nbsp; oReq = new XMLHttpRequest();else if (window.ActiveXObject)&nbsp; &nbsp; oReq = new ActiveXObject('MSXML2.XMLHTTP.3.0');else&nbsp; &nbsp; throw "XHR unavailable for your browser";var __excelurl = "filename.xlsx";if (__excelurl.toUpperCase().indexOf(".XLS") > -1 || __excelurl.toUpperCase().indexOf(".XLSX") > -1){&nbsp; &nbsp; oReq.open("GET", __excelurl, true);}xlsfile = false;function getdata(){&nbsp; &nbsp; if (__excelurl.toUpperCase().indexOf(".XLS") > -1 || __excelurl.toUpperCase().indexOf(".XLSX") > -1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (__excelurl.toUpperCase().indexOf(".XLSX") < 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xlsfile = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (typeof Uint8Array !== 'undefined')&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oReq.responseType = "arraybuffer";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oReq.onload = function (e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof console !== 'undefined')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("onload", new Date());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var arraybuffer = oReq.response;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = new Uint8Array(arraybuffer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var arr = new Array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i != data.length; ++i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = String.fromCharCode(data[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var wb = XLSX.read(arr.join(""), {type: "binary"});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process_wb(wb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oReq.setRequestHeader("Accept-Charset", "x-user-defined");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oReq.onreadystatechange = function ()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (oReq.readyState == 4 && oReq.status == 200)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ff = convertResponseBodyToText(oReq.responseBody);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof console !== 'undefined')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("onload", new Date());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var wb = XLSX.read(ff, {type: "binary"});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process_wb(wb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; oReq.send();&nbsp; &nbsp; } else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({url: __excelurl, aync: false, success: function (csvd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvdata = csvd;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to_csv()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "text",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; complete: function () {}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}getdata();现在将 Excel 数据转换为 csvvar finaldata = [];function process_wb(wb){&nbsp; &nbsp; icount = 0;&nbsp; &nbsp; inprocess = false;&nbsp; &nbsp; pcount = 0;&nbsp; &nbsp; output = to_csv(wb);}function to_csv(workbook){&nbsp; &nbsp; csvcount = 0;&nbsp; &nbsp; if (csvdata == "-1")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; csvdata = [];&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workbook.SheetNames.forEach(function (sheetName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xlsfile)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var csv = XLS.utils.sheet_to_csv(workbook.Sheets[sheetName]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (csv.length > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvtemp = csv.split("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (icsv = 0; icsv < csvtemp.length; icsv++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!csvtemp[icsv])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvcount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xdata = csvtemp[icsv];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvdata.push(xdata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; } catch (e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Format not compatible. Convert the data to Excel or CSV or Paste to see the details in the map.', 1, "Error !!!", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (csvdata.length > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvtemp = csvdata.split("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvdata = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (icsv = 0; icsv < csvtemp.length; icsv++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!csvtemp[icsv])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xdata = csvtemp[icsv];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvdata.push(xdata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (csvdata.length == 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; console.log("No data to generate chart");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; header = csvdata[0];&nbsp; &nbsp; header = header.split('"');&nbsp; &nbsp; header = header.join("")&nbsp; &nbsp; header.replace(/'"'/g, '');&nbsp; &nbsp; header = header.split(",");&nbsp; &nbsp; finaldata = [];&nbsp; &nbsp; for (idata = 1; idata < csvdata.length; idata++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; fdata = {};&nbsp; &nbsp; &nbsp; &nbsp; ftdata = csvdata[idata]&nbsp; &nbsp; &nbsp; &nbsp; ftx = ftdata.split('"');&nbsp; &nbsp; &nbsp; &nbsp; ftdata = ftx.join("")&nbsp; &nbsp; &nbsp; &nbsp; ftdata.replace(/'"'/g, '');&nbsp; &nbsp; &nbsp; &nbsp; ftdata = ftdata.split(',');&nbsp; &nbsp; &nbsp; &nbsp; for (ihead = 0; ihead < header.length; ihead++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!header[ihead])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftx = ftdata[ihead];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftx = ftx.replace(/'"'/g, "")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fdata[header[ihead]] = ftx;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finaldata.push(fdata);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}console.log(finaldata);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript