xlsx.utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var xlsxUtils = {
  2. Binary: {
  3. fixdata(data) { //文件流转BinaryString
  4. var o = "",
  5. l = 0,
  6. w = 10240;
  7. for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
  8. o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
  9. return o;
  10. },
  11. s2ab(s) { //字符串转字符流
  12. var buf = new ArrayBuffer(s.length);
  13. var view = new Uint8Array(buf);
  14. for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  15. return buf;
  16. }
  17. },
  18. _wb: null,
  19. _rABS: false,
  20. /**
  21. * @desc 导入根据文件
  22. * @param {File} f 文件
  23. * @param {Function} c 回调
  24. * @return {Object} 回调值
  25. */
  26. import(f, c) {
  27. this.wb = null;
  28. var reader = new FileReader();
  29. reader.onload = function (e) {
  30. var data = e.target.result;
  31. xlsxUtils._wb = xlsxUtils._rABS ? XLSX.read(btoa(xlsxUtils.Binary.fixdata(data)), { type: 'base64' }) : XLSX.read(data, { type: 'binary' });
  32. if (typeof c == "function") { c(xlsxUtils._wb); }
  33. };
  34. if (xlsxUtils._rABS) {
  35. reader.readAsArrayBuffer(f);
  36. } else {
  37. reader.readAsBinaryString(f);
  38. }
  39. },
  40. /**
  41. * @desc 根据表Sheet名获取数据
  42. * @param {String} name
  43. * @return {Object}
  44. */
  45. getSheetByName(name, opt) {//
  46. return XLSX.utils.sheet_to_json(xlsxUtils._wb.Sheets[name], opt);
  47. },
  48. /**
  49. * @desc 根据表Sheet索引获取数据
  50. * @param {Number} index
  51. * @return {Object}
  52. */
  53. getSheetByIndex(index = 0, opt) {
  54. return xlsxUtils.getSheetByName(xlsxUtils._wb.SheetNames[index], opt);
  55. },
  56. /**
  57. * @desc 导出
  58. * @param {Array} data 数据{title1:dataList,title2:dataList....}
  59. * @param {String} type
  60. * @return {Blob}
  61. */
  62. export(data, type) {
  63. var tmpWB = null;
  64. for (var title in data) {
  65. var tmpdata = xlsxUtils.format2Sheet(data[title]);
  66. tmpWB = xlsxUtils.format2WB(tmpdata, title, tmpWB);
  67. }
  68. return xlsxUtils.format2Blob(tmpWB, type);
  69. },
  70. /**
  71. * 从数据数组或对象中根据key生成相同key值的对象
  72. * @param {Object|Array} data
  73. * @return {Object}
  74. */
  75. readDataHead(data) {
  76. var o = {}, d = Array.isArray(data) ? Object.keys(data[0]) : data; for (var i of d) o[i] = i;
  77. return o;
  78. },
  79. /**
  80. * @desc 格式化数据为Sheet格式
  81. * @param {Array} json 数据
  82. * @param {Number} n 列偏移
  83. * @param {Number} r 行偏移
  84. * @param {Array} keyMap 对象键数组
  85. * @param {Function|Boolean} t 数据
  86. */
  87. format2Sheet(json, n, r, keyMap, t) {
  88. keyMap = keyMap || Object.keys(json[0]);
  89. var types = (t == undefined ? ((v) => (({ "number": "n", undefined: "s", "boolean": "b","string":"s" })[typeof v])||"s") : t);
  90. n = n || 0;
  91. r = r || 0;
  92. var tmpdata = {};//用来保存转换好的json
  93. var t1 = json.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
  94. v: v[k],
  95. position: ((j + n) > 25 ? xlsxUtils.getCharCol((j + n)) : String.fromCharCode(65 + (j + n))) + (i + 1 + r),
  96. }))).reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {
  97. v: v.v,
  98. t: types?types(v.v):"s"
  99. });
  100. return tmpdata;
  101. },
  102. /**
  103. * @desc 格式化数据为Sheet格式
  104. * @param {Array} sheetData
  105. * @param {String} title
  106. * @param {Object} wb
  107. * @param {Object} ref
  108. */
  109. format2WB(sheetData, title, wb, ref) {
  110. title = title || "mySheet";
  111. var outputPos = Object.keys(sheetData);
  112. if (!wb) wb = { Sheets: {}, SheetNames: [] };
  113. wb.SheetNames.push(title);
  114. wb.Sheets[title] = Object.assign({}, sheetData, {
  115. '!ref': ref || (outputPos[0] + ':' + outputPos.reverse().find(_=>_.indexOf("!")==-1))//设置填充区域
  116. });
  117. return wb;
  118. },
  119. /**
  120. * @desc 将xlsx Workbook 转为blob
  121. * @param {Array} wb
  122. * @param {String} type 类型
  123. */
  124. format2Blob(wb, type) {
  125. return new Blob([xlsxUtils.Binary.s2ab(XLSX.write(wb,
  126. { bookType: (type == undefined ? 'xlsx' : type), bookSST: false, type: 'binary' }//这里的数据是用来定义导出的格式类型
  127. ))], { type: "" });
  128. },
  129. /**
  130. * @desc 匹配单元格对应的标识
  131. * @param {Number} n
  132. */
  133. getCharCol(n) {
  134. let temCol = '',
  135. s = '',
  136. m = 0
  137. while (n > 0) {
  138. m = n % 26 + 1
  139. s = String.fromCharCode(m + 64) + s
  140. n = (n - m) / 26
  141. }
  142. return s
  143. },
  144. };