553326fc12dc58649469a5cef1c142db4a6a42f9.svn-base 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* eslint-disable */
  2. require('script-loader!file-saver');
  3. import XLSX from 'xlsx'
  4. function generateArray(table) {
  5. var out = [];
  6. var rows = table.querySelectorAll('tr');
  7. var ranges = [];
  8. for (var R = 0; R < rows.length; ++R) {
  9. var outRow = [];
  10. var row = rows[R];
  11. var columns = row.querySelectorAll('td');
  12. for (var C = 0; C < columns.length; ++C) {
  13. var cell = columns[C];
  14. var colspan = cell.getAttribute('colspan');
  15. var rowspan = cell.getAttribute('rowspan');
  16. var cellValue = cell.innerText;
  17. if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
  18. //Skip ranges
  19. ranges.forEach(function (range) {
  20. if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
  21. for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
  22. }
  23. });
  24. //Handle Row Span
  25. if (rowspan || colspan) {
  26. rowspan = rowspan || 1;
  27. colspan = colspan || 1;
  28. ranges.push({
  29. s: {
  30. r: R,
  31. c: outRow.length
  32. },
  33. e: {
  34. r: R + rowspan - 1,
  35. c: outRow.length + colspan - 1
  36. }
  37. });
  38. };
  39. //Handle Value
  40. outRow.push(cellValue !== "" ? cellValue : null);
  41. //Handle Colspan
  42. if (colspan)
  43. for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
  44. }
  45. out.push(outRow);
  46. }
  47. return [out, ranges];
  48. };
  49. function datenum(v, date1904) {
  50. if (date1904) v += 1462;
  51. var epoch = Date.parse(v);
  52. return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  53. }
  54. function sheet_from_array_of_arrays(data, opts) {
  55. var ws = {};
  56. var range = {
  57. s: {
  58. c: 10000000,
  59. r: 10000000
  60. },
  61. e: {
  62. c: 0,
  63. r: 0
  64. }
  65. };
  66. for (var R = 0; R != data.length; ++R) {
  67. for (var C = 0; C != data[R].length; ++C) {
  68. if (range.s.r > R) range.s.r = R;
  69. if (range.s.c > C) range.s.c = C;
  70. if (range.e.r < R) range.e.r = R;
  71. if (range.e.c < C) range.e.c = C;
  72. var cell = {
  73. v: data[R][C]
  74. };
  75. if (cell.v == null) continue;
  76. var cell_ref = XLSX.utils.encode_cell({
  77. c: C,
  78. r: R
  79. });
  80. if (typeof cell.v === 'number') cell.t = 'n';
  81. else if (typeof cell.v === 'boolean') cell.t = 'b';
  82. else if (cell.v instanceof Date) {
  83. cell.t = 'n';
  84. cell.z = XLSX.SSF._table[14];
  85. cell.v = datenum(cell.v);
  86. } else cell.t = 's';
  87. ws[cell_ref] = cell;
  88. }
  89. }
  90. if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
  91. return ws;
  92. }
  93. function Workbook() {
  94. if (!(this instanceof Workbook)) return new Workbook();
  95. this.SheetNames = [];
  96. this.Sheets = {};
  97. }
  98. function s2ab(s) {
  99. var buf = new ArrayBuffer(s.length);
  100. var view = new Uint8Array(buf);
  101. for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  102. return buf;
  103. }
  104. export function export_table_to_excel(id) {
  105. var theTable = document.getElementById(id);
  106. var oo = generateArray(theTable);
  107. var ranges = oo[1];
  108. /* original data */
  109. var data = oo[0];
  110. var ws_name = "SheetJS";
  111. var wb = new Workbook(),
  112. ws = sheet_from_array_of_arrays(data);
  113. /* add ranges to worksheet */
  114. // ws['!cols'] = ['apple', 'banan'];
  115. ws['!merges'] = ranges;
  116. /* add worksheet to workbook */
  117. wb.SheetNames.push(ws_name);
  118. wb.Sheets[ws_name] = ws;
  119. var wbout = XLSX.write(wb, {
  120. bookType: 'xlsx',
  121. bookSST: false,
  122. type: 'binary'
  123. });
  124. saveAs(new Blob([s2ab(wbout)], {
  125. type: "application/octet-stream"
  126. }), "test.xlsx")
  127. }
  128. export function export_json_to_excel({
  129. multiHeader = [],
  130. header,
  131. data,
  132. filename,
  133. merges = [],
  134. autoWidth = true,
  135. bookType= 'xlsx',
  136. ws_name,
  137. } = {}) {
  138. /* original data */
  139. filename = filename || 'excel-list'
  140. data = [...data]
  141. data.unshift(header);
  142. ws_name = ws_name || "SheetJS"
  143. for (let i = multiHeader.length-1; i > -1; i--) {
  144. data.unshift(multiHeader[i])
  145. }
  146. // var ws_name = "SheetJS";
  147. var wb = new Workbook(),
  148. ws = sheet_from_array_of_arrays(data);
  149. if (merges.length > 0) {
  150. if (!ws['!merges']) ws['!merges'] = [];
  151. merges.forEach(item => {
  152. ws['!merges'].push(XLSX.utils.decode_range(item))
  153. })
  154. }
  155. if (autoWidth) {
  156. /*设置worksheet每列的最大宽度*/
  157. const colWidth = data.map(row => row.map(val => {
  158. /*先判断是否为null/undefined*/
  159. if (val == null) {
  160. return {
  161. 'wch': 10
  162. };
  163. }
  164. /*再判断是否为中文*/
  165. else if (val.toString().charCodeAt(0) > 255) {
  166. return {
  167. 'wch': val.toString().length * 2
  168. };
  169. } else {
  170. return {
  171. 'wch': val.toString().length
  172. };
  173. }
  174. }))
  175. /*以第一行为初始值*/
  176. let result = colWidth[0];
  177. for (let i = 1; i < colWidth.length; i++) {
  178. for (let j = 0; j < colWidth[i].length; j++) {
  179. if (result[j]['wch'] < colWidth[i][j]['wch']) {
  180. result[j]['wch'] = colWidth[i][j]['wch'];
  181. }
  182. }
  183. }
  184. ws['!cols'] = result;
  185. }
  186. /* add worksheet to workbook */
  187. wb.SheetNames.push(ws_name);
  188. wb.Sheets[ws_name] = ws;
  189. var wbout = XLSX.write(wb, {
  190. bookType: bookType,
  191. bookSST: false,
  192. type: 'binary'
  193. });
  194. saveAs(new Blob([s2ab(wbout)], {
  195. type: "application/octet-stream"
  196. }), `${filename}.${bookType}`);
  197. }
  198. // 多项导出
  199. export function export_json_to_excel2({
  200. multiHeader = [],
  201. header,
  202. data,
  203. sheetname,
  204. filename,
  205. merges = [],
  206. autoWidth = true,
  207. bookType = "xlsx"
  208. } = {}) {
  209. /* original data */
  210. filename = filename || "excel-list"
  211. data = [...data]
  212. for (var i = 0; i < header.length; i++) {
  213. data[i].unshift(header[i])
  214. }
  215. // data.unshift(header)
  216. for (let i = multiHeader.length - 1; i > -1; i--) {
  217. data.unshift(multiHeader[i])
  218. }
  219. var ws_name = sheetname
  220. var wb = new Workbook(),
  221. ws = []
  222. for (var j = 0; j < header.length; j++) {
  223. ws.push(sheet_from_array_of_arrays(data[j]))
  224. }
  225. if (merges.length > 0) {
  226. if (!ws["!merges"]) ws["!merges"] = []
  227. merges.forEach(item => {
  228. ws["!merges"].push(XLSX.utils.decode_range(item))
  229. })
  230. }
  231. // console.log("width", autoWidth)
  232. if (autoWidth) {
  233. /*设置worksheet每列的最大宽度*/
  234. var colWidth = []
  235. for (var k = 0; k < header.length; k++) {
  236. colWidth.push(
  237. data[k].map(row =>
  238. row.map(val => {
  239. /*先判断是否为null/undefined*/
  240. if (val == null) {
  241. return {
  242. wch: 10
  243. }
  244. } else if (val.toString().charCodeAt(0) > 255) {
  245. /*再判断是否为中文*/
  246. return {
  247. wch: val.toString().length * 2
  248. }
  249. } else {
  250. return {
  251. wch: val.toString().length
  252. }
  253. }
  254. })
  255. )
  256. )
  257. }
  258. /*以第一行为初始值*/
  259. let result = []
  260. for (var k = 0; k < colWidth.length; k++) {
  261. result[k] = colWidth[k][0]
  262. for (let i = 1; i < colWidth[k].length; i++) {
  263. for (let j = 0; j < colWidth[k][i].length; j++) {
  264. if (result[k][j]["wch"] < colWidth[k][i][j]["wch"]) {
  265. result[k][j]["wch"] = colWidth[k][i][j]["wch"]
  266. }
  267. }
  268. }
  269. }
  270. // 分别给sheet表设置宽度
  271. for (var l = 0; l < result.length; l++) {
  272. ws[l]["!cols"] = result[l]
  273. }
  274. }
  275. /* add worksheet to workbook */
  276. for (var k = 0; k < header.length; k++) {
  277. wb.SheetNames.push(ws_name[k])
  278. wb.Sheets[ws_name[k]] = ws[k]
  279. }
  280. var wbout = XLSX.write(wb, {
  281. bookType: bookType,
  282. bookSST: false,
  283. type: "binary"
  284. })
  285. saveAs(
  286. new Blob([s2ab(wbout)], {
  287. type: "application/octet-stream"
  288. }),
  289. `${filename}.${bookType}`
  290. )
  291. }