Browse Source

前端,导入Excel提取共用窗口

MaiXinRong 5 năm trước cách đây
mục cha
commit
6f500b658a

+ 53 - 0
app/controller/template_controller.js

@@ -0,0 +1,53 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const path = require('path');
+const fs = require('fs');
+
+module.exports = app => {
+
+    class TemplateController extends app.BaseController {
+        /**
+         * 下载 各种模板
+         * @param ctx
+         * @returns {Promise<void>}
+         */
+        async download(ctx) {
+            const file = ctx.params.file;
+            if (file) {
+                try {
+                    let fileName;
+                    switch (file) {
+                        case '导入分项清单EXCEL格式.xls':
+                            fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入分项清单EXCEL格式.xls');
+                            break;
+                        case '导入工程量清单EXCEL格式.xls':
+                            fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入工程量清单EXCEL格式.xls');
+                            break;
+                        default:
+                            throw '参数错误'
+                    }
+                    ctx.body = await fs.readFileSync(fileName);
+                } catch (err) {
+                    this.log(err);
+                    if (err.stack) {
+                        ctx.body = '您下载的示例文件不存在';
+                    } else {
+                        ctx.body = err;
+                    }
+                }
+            } else {
+                ctx.body = '参数错误';
+            }
+        }
+    }
+
+    return TemplateController;
+};

BIN
app/public/files/template/ledger/导入工程量清单EXCEL格式.xls


+ 43 - 64
app/public/js/ledger.js

@@ -1135,7 +1135,49 @@ $(document).ready(function() {
                         return readOnly;
                     },
                     callback: function (key, opt) {
-                        $('#upload-ledger').modal('show');
+                        importExcel.doImport({
+                            template: {
+                                hint: '0号台账',
+                                url: '/template/导入分项清单EXCEL格式.xls',
+                            },
+                            callback: function (sheet) {
+                                postDataCompress(window.location.pathname + '/upload-excel', sheet, function (result) {
+                                    ledgerTree.loadDatas(result.bills);
+                                    treeCalc.calculateAll(ledgerTree);
+                                    SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
+                                    pos.loadDatas(result.pos);
+                                    posOperationObj.loadCurPosData();
+                                }, null);
+                            }
+                        });
+                        //$('#upload-ledger').modal('show');
+                    }
+                },
+                'importGclBills2Xmj': {
+                    name: '导入工程量清单至项目节',
+                    icon: 'fa-file-excel-o',
+                    disabled: function (key, opt) {
+                        const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
+                        return readOnly
+                            || (node.children && node.children.length > 0)
+                            || (!_.isNil(node.b_code) && node.b_code !== '');
+                    },
+                    callback: function (key, opt) {
+                        importExcel.doImport({
+                            template: {
+                                hint: '工程量清单',
+                                url: '/template/导入工程量清单EXCEL格式.xls',
+                            },
+                            callback: function (sheet) {
+                                postDataCompress(window.location.pathname + '/upload-excel', sheet, function (result) {
+                                    ledgerTree.loadDatas(result.bills);
+                                    treeCalc.calculateAll(ledgerTree);
+                                    SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
+                                    pos.loadDatas(result.pos);
+                                    posOperationObj.loadCurPosData();
+                                }, null);
+                            }
+                        });
                     }
                 },
                 'exportExcel': {
@@ -2206,69 +2248,6 @@ $(document).ready(function() {
             }
         });
     });
-    // 选择excel文件后,加载全部sheet
-    $('#upload-ledger-file').change(function () {
-        if (this.files.length === 0) {
-            $('#upload-ledger-sheets').html('').hide();
-            return;
-        }$('#upload-ledger-sheets').hide();
-        try {
-            $('#select-excel-loading').show();
-            xlsxUtils.import(this.files[0], (excelData) => {
-                if (excelData.SheetNames.length > 0) {
-                    const html = [];
-                    html.push('<hr></hr>');
-                    html.push('<h6>选择导入的工作表</h6>');
-                    for (const iName in excelData.SheetNames) {
-                        const name = excelData.SheetNames[iName];
-                        html.push('<div class="card p-2 mb-2">');
-                        html.push('<div class="form-check">');
-                        html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"',  '>');
-                        html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
-                        html.push('</div>');
-                        html.push('</div>');
-                    }
-                    $('#upload-ledger-sheets').html(html.join('')).show();
-                    $('.mb-2.p-2').mouseenter(function () {
-                        $(this).addClass('border-primary');
-                    });
-                    $('.mb-2.p-2').mouseleave(function () {
-                        $(this).removeClass('border-primary');
-                    })
-                } else {
-                    toastr.info('选择的Excel无有效数据,请重新选择');
-                    $('#upload-ledger-sheets').hide();
-                }
-                $('#select-excel-loading').hide();
-            });
-        } catch(err) {
-            $('#select-excel-loading').hide();
-            toastr.error('加载excel异常,请刷新当前页面');
-            $('#upload-ledger-sheets').hide();
-        }
-    });
-    // 上传excel内容,并导入
-    $('#upload-ledger-ok').click(function () {
-        const sheetName = $('input[name=sheetName]:checked').val();
-        if (sheetName) {
-            const sheet = {
-                rows: xlsxUtils.getSheetByName(sheetName, {header: 1}),
-                merge: xlsxUtils._wb.Sheets[sheetName]["!merges"]
-            };
-            postDataCompress(window.location.pathname + '/upload-excel', sheet, function (result) {
-                ledgerTree.loadDatas(result.bills);
-                treeCalc.calculateAll(ledgerTree);
-                SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
-                pos.loadDatas(result.pos);
-                posOperationObj.loadCurPosData();
-                $('#upload-ledger').modal('hide');
-            }, null);
-        }
-    });
-    $('#upload-ledger').bind('hidden.bs.modal', function () {
-        $('#upload-ledger-file').val('');
-        $('#upload-ledger-sheets').html('');
-    });
 
     $('#hideSp').click(function () {
         $('#sp-list2').modal('hide');

+ 3 - 0
app/router.js

@@ -255,4 +255,7 @@ module.exports = app => {
 
     // 查询
     app.post('/search/user', sessionAuth, 'projectController.searchAccount');
+
+    // 示例
+    app.get('/template/:file', sessionAuth, 'templateController.download');
 };

+ 2 - 1
app/view/ledger/explode_modal.ejs

@@ -401,4 +401,5 @@
     </div>
 </div>
 <% } %>
-<% include ../shares/merge_peg_modal.ejs %>
+<% include ../shares/merge_peg_modal.ejs %>
+<% include ../shares/import_excel_modal.ejs %>

+ 100 - 0
app/view/shares/import_excel_modal.ejs

@@ -0,0 +1,100 @@
+<!--导入清单Excel-->
+<div class="modal fade" id="import-excel" data-backdrop="static" enctype="multipart/form-data">
+    <div class="modal-dialog" role="document">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h5 class="modal-title">导入</h5>
+            </div>
+            <div class="modal-body">
+                <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>
+                <div class="form-group">
+                    <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>
+                    <input type="file" class="form-control-file" id="import-excel-file" accept="*.xls">
+                </div>
+                <div id="excel-sheets" style="display: none;">
+                    <hr></hr>
+                    <h6>选择导入的工作表</h6>
+                </div>
+            </div>
+            <div class="modal-footer">
+                <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
+                <button type="button" class="btn btn-primary btn-sm" id="import-excel-ok">确认上传</button>
+            </div>
+        </div>
+    </div>
+</div>
+<script>
+    const importExcel = (function () {
+        // 选择excel文件后,加载全部sheet
+        $('#import-excel-file').change(function () {
+            if (this.files.length === 0) {
+                $('#excel-sheets').html('').hide();
+                return;
+            }
+            $('#excel-sheets').hide();
+            try {
+                $('#select-excel-loading').show();
+                xlsxUtils.import(this.files[0], (excelData) => {
+                    if (excelData.SheetNames.length > 0) {
+                        const html = [];
+                        html.push('<hr></hr>');
+                        html.push('<h6>选择导入的工作表</h6>');
+                        for (const iName in excelData.SheetNames) {
+                            const name = excelData.SheetNames[iName];
+                            html.push('<div class="card p-2 mb-2">');
+                            html.push('<div class="form-check">');
+                            html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"',  '>');
+                            html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
+                            html.push('</div>');
+                            html.push('</div>');
+                        }
+                        $('#excel-sheets').html(html.join('')).show();
+                        $('.mb-2.p-2').mouseenter(function () {
+                            $(this).addClass('border-primary');
+                        });
+                        $('.mb-2.p-2').mouseleave(function () {
+                            $(this).removeClass('border-primary');
+                        })
+                    } else {
+                        toastr.info('选择的Excel无有效数据,请重新选择');
+                        $('#excel-sheets').hide();
+                    }
+                    $('#select-excel-loading').hide();
+                });
+            } catch(err) {
+                $('#select-excel-loading').hide();
+                toastr.error('加载excel异常,请刷新当前页面');
+                $('#excel-sheets').hide();
+            }
+        });
+        // 清除上一次数据
+        $('#upload-ledger').bind('hidden.bs.modal', function () {
+            $('#import-excel-file').val('');
+            $('#excel-sheets').html('');
+        });
+
+        const doImport = function (setting) {
+            // 上传excel内容,并导入
+            $('#import-excel-ok').click(function () {
+                const sheetName = $('input[name=sheetName]:checked').val();
+                if (sheetName) {
+                    const sheet = {
+                        rows: xlsxUtils.getSheetByName(sheetName, {header: 1}),
+                        merge: xlsxUtils._wb.Sheets[sheetName]["!merges"]
+                    };
+                    setting.callback(sheet);
+                    $('#import-excel').modal('hide');
+                }
+            });
+            if (setting.template) {
+                $('#import-template').show();
+                $('#import-type-hint').html(setting.template.hint);
+                $('#download-template').attr('href', setting.template.url);
+            } else {
+                $('#import-template').hide();
+            }
+            $('#import-excel').modal('show');
+        }
+        return { doImport };
+    })();
+</script>

+ 1 - 1
app/view/shares/merge_peg_modal.ejs

@@ -9,7 +9,7 @@
                 <div class="mb-2" style="height: 25px; line-height: 25px">
                     <div class="d-inline-block mr-3">
                         <div class="form-check">
-                            <input class="form-check-input" type="checkbox" value="" id="mp-with-pos">
+                            <input class="form-check-input" type="checkbox" valu e="" id="mp-with-pos">
                             <label class="form-check-label" for="defaultCheck1">
                                 合并位置
                             </label>