import_excel_modal.ejs 4.9 KB

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