import_excel_modal.ejs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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">
  21. <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
  22. <button type="button" class="btn btn-primary btn-sm" id="import-excel-ok">确认上传</button>
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. <script>
  28. const importExcel = (function () {
  29. const uploadType = {
  30. file: 1,
  31. data: 2
  32. }
  33. let callback, u_type;
  34. // 选择excel文件后,加载全部sheet
  35. $('#import-excel-file').change(function () {
  36. if (this.files.length === 0) {
  37. $('#excel-sheets').html('').hide();
  38. return;
  39. }
  40. $('#excel-sheets').hide();
  41. try {
  42. $('#select-excel-loading').show();
  43. xlsxUtils.import(this.files[0], (excelData) => {
  44. if (excelData.SheetNames.length > 0) {
  45. const html = [];
  46. html.push('<hr></hr>');
  47. html.push('<h6>选择导入的工作表</h6>');
  48. for (const iName in excelData.SheetNames) {
  49. const name = excelData.SheetNames[iName];
  50. html.push('<div class="card p-2 mb-2">');
  51. html.push('<div class="form-check">');
  52. html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"', '>');
  53. html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
  54. html.push('</div>');
  55. html.push('</div>');
  56. }
  57. $('#excel-sheets').html(html.join('')).show();
  58. $('.mb-2.p-2').mouseenter(function () {
  59. $(this).addClass('border-primary');
  60. });
  61. $('.mb-2.p-2').mouseleave(function () {
  62. $(this).removeClass('border-primary');
  63. })
  64. } else {
  65. toastr.info('选择的Excel无有效数据,请重新选择');
  66. $('#excel-sheets').hide();
  67. }
  68. $('#select-excel-loading').hide();
  69. });
  70. } catch(err) {
  71. $('#select-excel-loading').hide();
  72. toastr.error('加载excel异常,请刷新当前页面');
  73. $('#excel-sheets').hide();
  74. }
  75. });
  76. // 清除上一次数据
  77. $('#import-excel').bind('hidden.bs.modal', function () {
  78. $('#import-excel-file').val('');
  79. $('#excel-sheets').html('');
  80. });
  81. // 上传excel内容,并导入
  82. $('#import-excel-ok').click(function () {
  83. const sheetName = $('input[name=sheetName]:checked').val();
  84. if (sheetName) {
  85. if (u_type === uploadType.file) {
  86. if (callback) callback($('#import-excel-file')[0], sheetName);
  87. } else if (u_type === uploadType.data) {
  88. const sheet = {
  89. rows: xlsxUtils.getSheetByName(sheetName, {header: 1}),
  90. merge: xlsxUtils._wb.Sheets[sheetName]["!merges"]
  91. };
  92. if (callback) callback(sheet);
  93. }
  94. $('#import-excel').modal('hide');
  95. }
  96. });
  97. const doImport = function (setting) {
  98. if (setting.template) {
  99. $('#import-template').show();
  100. $('#import-type-hint').html(setting.template.hint);
  101. $('#download-template').attr('href', setting.template.url);
  102. } else {
  103. $('#import-template').hide();
  104. }
  105. $('#import-excel').modal('show');
  106. callback = setting.callback;
  107. u_type = setting.u_type ? setting.u_type : uploadType.data;
  108. }
  109. return { doImport, uploadType };
  110. })();
  111. </script>