import_excel_modal.ejs 6.0 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" href="/tender/<%- ctx.tender.id %>/ledger/download/导入分项清单EXCEL格式.xls">下载示例</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. <hr></hr>
  17. <h6>选择导入的工作表</h6>
  18. </div>
  19. </div>
  20. <div class="modal-footer d-flex justify-content-between">
  21. <div>
  22. <div class="custom-control custom-checkbox custom-control-inline" id="filter-excel">
  23. <input type="radio" class="custom-control-input" name="customRadioInline1" id="filter-qty-price" checked="">
  24. <label class="custom-control-label" for="filter-qty-price">过滤清单数量和单价为0项</label>
  25. </div>
  26. </div>
  27. <div>
  28. <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
  29. <button type="button" class="btn btn-primary btn-sm" id="import-excel-ok">确认上传</button>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <script>
  36. const importExcel = (function () {
  37. const uploadType = {
  38. file: 1,
  39. data: 2
  40. }
  41. let callback, u_type;
  42. // 选择excel文件后,加载全部sheet
  43. $('#import-excel-file').change(function () {
  44. if (this.files.length === 0) {
  45. $('#excel-sheets').html('').hide();
  46. return;
  47. }
  48. $('#excel-sheets').hide();
  49. try {
  50. $('#select-excel-loading').show();
  51. xlsxUtils.import(this.files[0], (excelData) => {
  52. if (excelData.SheetNames.length > 0) {
  53. const html = [];
  54. html.push('<hr></hr>');
  55. html.push('<h6>选择导入的工作表</h6>');
  56. for (const iName in excelData.SheetNames) {
  57. const name = excelData.SheetNames[iName];
  58. html.push('<div class="card p-2 mb-2">');
  59. html.push('<div class="form-check">');
  60. html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"', '>');
  61. html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
  62. html.push('</div>');
  63. html.push('</div>');
  64. }
  65. $('#excel-sheets').html(html.join('')).show();
  66. $('.mb-2.p-2').mouseenter(function () {
  67. $(this).addClass('border-primary');
  68. });
  69. $('.mb-2.p-2').mouseleave(function () {
  70. $(this).removeClass('border-primary');
  71. })
  72. } else {
  73. toastr.info('选择的Excel无有效数据,请重新选择');
  74. $('#excel-sheets').hide();
  75. }
  76. $('#select-excel-loading').hide();
  77. });
  78. } catch(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>