import_excel_modal.ejs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!--依赖xlsx/jQuery-->
  2. <!--导入清单Excel-->
  3. <div class="modal fade" id="import-excel" data-backdrop="static" enctype="multipart/form-data">
  4. <div class="modal-dialog" role="document">
  5. <div class="modal-content">
  6. <div class="modal-header">
  7. <h5 class="modal-title">导入</h5>
  8. </div>
  9. <div class="modal-body">
  10. <p id="import-template">请上传符合 <b id="import-type-hint">0号台账</b> 格式的 .xls和.xlsx 文件,<a id="download-template">下载示例</a>。</p>
  11. <div class="form-group">
  12. <label for="exampleFormControlFile1">选择文件</label><i class="fa fa-spinner fa-pulse fa-lg fa-fw text-primary" id="select-excel-loading" style="display: none;"></i>
  13. <input type="file" class="form-control-file" id="import-excel-file" accept="*.xls">
  14. </div>
  15. <div id="excel-sheets" style="display: none;">
  16. <h6>选择导入的工作表</h6>
  17. </div>
  18. </div>
  19. <div class="modal-footer d-flex justify-content-between">
  20. <div>
  21. <div class="custom-control custom-checkbox custom-control-inline" id="filter-excel">
  22. <input type="checkbox" class="custom-control-input" name="customRadioInline1" id="filter-qty-price" checked="">
  23. <label class="custom-control-label" for="filter-qty-price">过滤清单数量和单价为0项</label>
  24. </div>
  25. </div>
  26. <div>
  27. <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
  28. <button type="button" class="btn btn-primary btn-sm" id="import-excel-ok">确认上传</button>
  29. </div>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <script>
  35. const importExcel = (function () {
  36. const uploadType = {
  37. file: 1,
  38. data: 2
  39. }
  40. let callback, u_type;
  41. // 选择excel文件后,加载全部sheet
  42. $('#import-excel-file').change(function () {
  43. if (this.files.length === 0) {
  44. $('#excel-sheets').html('').hide();
  45. return;
  46. }
  47. $('#excel-sheets').hide();
  48. try {
  49. $('#select-excel-loading').show();
  50. xlsxUtils.import(this.files[0], (excelData) => {
  51. if (excelData.SheetNames.length > 0) {
  52. const html = [];
  53. html.push('<hr></hr>');
  54. html.push('<h6>选择导入的工作表</h6>');
  55. for (const iName in excelData.SheetNames) {
  56. const name = excelData.SheetNames[iName];
  57. html.push('<div class="card p-2 mb-2">');
  58. html.push('<div class="form-check">');
  59. html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"', '>');
  60. html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
  61. html.push('</div>');
  62. html.push('</div>');
  63. }
  64. $('#excel-sheets').html(html.join('')).show();
  65. $('.mb-2.p-2').mouseenter(function () {
  66. $(this).addClass('border-primary');
  67. });
  68. $('.mb-2.p-2').mouseleave(function () {
  69. $(this).removeClass('border-primary');
  70. })
  71. } else {
  72. toastr.info('选择的Excel无有效数据,请重新选择');
  73. $('#excel-sheets').hide();
  74. }
  75. $('#select-excel-loading').hide();
  76. });
  77. } catch(err) {
  78. console.log(err);
  79. $('#select-excel-loading').hide();
  80. toastr.error('加载excel异常,请刷新当前页面');
  81. $('#excel-sheets').hide();
  82. }
  83. });
  84. // 清除上一次数据
  85. $('#import-excel').bind('hidden.bs.modal', function () {
  86. $('#import-excel-file').val('');
  87. $('#excel-sheets').html('');
  88. });
  89. // 上传excel内容,并导入
  90. $('#import-excel-ok').click(function () {
  91. const sheetName = $('input[name=sheetName]:checked').val();
  92. if (sheetName) {
  93. if (u_type === uploadType.file) {
  94. if (callback) callback($('#import-excel-file')[0], sheetName);
  95. } else if (u_type === uploadType.data) {
  96. const sheet = {
  97. rows: xlsxUtils.getSheetByName(sheetName, {header: 1}),
  98. merge: xlsxUtils._wb.Sheets[sheetName]["!merges"]
  99. };
  100. if (callback) callback(sheet, $('#filter-qty-price')[0].checked);
  101. }
  102. $('#import-excel').modal('hide');
  103. }
  104. });
  105. const doImport = function (setting) {
  106. if (setting.template) {
  107. $('#import-template').show();
  108. $('#import-type-hint').html(setting.template.hint);
  109. $('#download-template').attr('href', setting.template.url);
  110. } else {
  111. $('#import-template').hide();
  112. }
  113. if (setting.filter) {
  114. $('#filter-excel').show();
  115. } else {
  116. $('#filter-excel').hide();
  117. }
  118. $('#import-excel').modal('show');
  119. callback = setting.callback;
  120. u_type = setting.u_type ? setting.u_type : uploadType.data;
  121. }
  122. return { doImport, uploadType };
  123. })();
  124. </script>