JS客户端读取Excel文件插件js-xls使用方法

js-xls是一款客户端读取Excel的插件,亲测IE11、FireFox、Chrome可用,读取速度也可观。

插件Demo地址:http://oss.sheetjs.com/js-xlsx/    http://oss.sheetjs.com/js-xls/

Excel列信息

js代码如下:

  1 var X = XLS;
  2 function to_json(workbook) {
  3     var result = {};
  4     workbook.SheetNames.forEach(function (sheetName) {
  5         var roa = XLS.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
  6         if (roa.length > 0) {
  7             result[sheetName] = roa;
  8         }
  9     });
 10     return result;
 11 }
 12 
 13 function fixdata(data) {
 14     var o = "", l = 0, w = 10240;
 15     for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
 16     o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
 17     return o;
 18 }
 19 
 20 function process_wb(wb,type) {
 21     var output = "";
 22     switch (type) {
 23         case "json":
 24             output = JSON.stringify(to_json(wb), 2, 2);
 25             break;
 26         case "form":
 27             output = to_formulae(wb);
 28             break;
 29         default:
 30             output = to_csv(wb);
 31     }
 32     //if (out.innerText === undefined) out.textContent = output;
 33     //else out.innerText = output;
 34     return output;
 35 } 
47
48 function ReadExcel(out) { 49 var selCodes = $("#selProductCodes").val(); 50 var exlData = JSON.parse(out); 51 var regCode = /^[1-9]\d*$/; 52 var regQty = /^\d*$/; 53 var ids = []; 54 var msg = ""; 55 if (exlData.Sheet1 == null || exlData.Sheet1 == undefined) { 56 msgErro("未查询到Excel文件中的数据!"); 57 saveLoading('hide'); 58 return; 59 } 60 for (var i = 0; i < exlData.Sheet1.length; i++) { 61 var sysno = exlData.Sheet1[i].ProductSysNo; 62 var qty = exlData.Sheet1[i].Qty; 63 if (sysno == undefined) 64 msg += "Qty列数据为 " + qty + " 的行,缺少ProductSysNo信息!\r\n"; 65 else if (qty == undefined) 66 msg += "ProductSysNo列数据为 " + sysno + " 的行,缺少Qty信息!\r\n"; 67 else if (!regCode.test(sysno)) 68 msg += "ProductSysNo列数据为 " + sysno + " 不是有效的商品编号!\r\n"; 69 else if (!regQty.test(qty)) 70 msg += "Qty列数据为 " + qty + " 不是有效的数量格式!\r\n"; 71 else { 72 ids.push(sysno); 73 } 74 }
     //do something
84 } 85 var xlf = document.getElementById('xlf'); //<input type="file" id="xlf" /> 86 function handleFile(e) { 87 if (e.target.files[0].name.indexOf(".xls") < 0) { 88 msgErro("请选择.xls格式文件!"); 89 return; 90 } 91 saveLoading('show'); 92 rABS = false; 93 use_worker = false; 94 var files = e.target.files; 95 var output = ""; 96 var f = files[0]; 97 { 98 var reader = new FileReader(); 99 var name = f.name; 100 reader.onload = function (e) { 101 if (typeof console !== 'undefined') console.log("onload", new Date(), rABS, use_worker); 102 var data = e.target.result; 103 if (use_worker) { 104 xw(data, process_wb); 105 } else { 106 var wb; 107 if (rABS) { 108 wb = X.read(data, { type: 'binary' }); 109 } else { 110 var arr = fixdata(data); 111 wb = X.read(btoa(arr), { type: 'base64' }); 112 } 113 output = process_wb(wb, "json"); 114 ReadExcel(output); 115 } 116 }; 117 if (rABS) reader.readAsBinaryString(f); 118 else reader.readAsArrayBuffer(f); 119 } 120 } 121 122 if (xlf.addEventListener) xlf.addEventListener('change', handleFile, false);

 js-xls插件下载地址:下载插件

posted @ 2016-12-30 17:52  追逐者——Eagle  阅读(20709)  评论(4编辑  收藏  举报