Sfoglia il codice sorgente

kmall导出excel优化

lhm 3 anni fa
parent
commit
5129c06b3c

+ 97 - 48
kmall-admin/src/main/java/com/kmall/admin/controller/OrderController.java

@@ -11,6 +11,7 @@ import com.kmall.admin.utils.ShiroUtils;
 import com.kmall.common.constant.Dict;
 import com.kmall.common.constant.JxlsXmlTemplateName;
 import com.kmall.common.utils.*;
+import com.kmall.common.utils.excel.Entry;
 import com.kmall.common.utils.excel.ExcelExport;
 import com.kmall.common.utils.excel.ExcelUtil;
 import com.kmall.common.utils.print.ticket.item.Ticket;
@@ -1346,8 +1347,10 @@ public class OrderController {
                 "Brand","EDLP","Current Price","Cost Price",/*"Prime Cost","Goods Number",*/"Deduction Rate","Unit Sold","Sales","Tax amount","Total Sales incl. Tax","Tax Rate",
                 "GP ¥","GP %","Product Category","Supplier Name","Transaction Type","Sale Return Type","Remark"};
 
-        LinkedHashMap<String, Object> headerMap = initHeaderMap();
-        List<Map<String, Object>> list = new LinkedList<>();
+//        LinkedHashMap<String, Object> headerMap = initHeaderMap();
+        List<Entry> headerMap = initHeaderMap();
+//        List<Map<String, Object>> list = new LinkedList<>();
+        List<List<Entry>> list = new ArrayList<>();
         list.add(headerMap);
         if (systemFormatList != null && systemFormatList.size() > 0) {
             for (SystemFormatDto systemFormat : systemFormatList) {
@@ -1377,7 +1380,8 @@ public class OrderController {
 //                }
 
 
-                LinkedHashMap<String, Object> map = new LinkedHashMap<>(32);
+//                LinkedHashMap<String, Object> map = new LinkedHashMap<>(32);
+                List<Entry> entries = new ArrayList<>();
                 if (Objects.nonNull(systemFormat.getTaxRate()) && Objects.nonNull(systemFormat.getTotalSalesInclTax())){
 
 
@@ -1445,7 +1449,7 @@ public class OrderController {
                     primeCost = gp.setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                     systemFormat.setPrimeCost(primeCost);
                 }
-                map.put("ReceiptNo",systemFormat.getReceiptNo());
+                /*map.put("ReceiptNo",systemFormat.getReceiptNo());
                 map.put("StoreName",systemFormat.getStoreName());
                 map.put("CashRegisterNo",systemFormat.getCashRegisterNo());
                 map.put("TimeStampDetails",systemFormat.getTimeStampDetails());
@@ -1487,62 +1491,107 @@ public class OrderController {
 
                 map.put("Remark",systemFormat.getRemark());
 
-                list.add(map);
+                list.add(map);*/
+
+                entries.add(new Entry("ReceiptNo",systemFormat.getReceiptNo()));
+                entries.add(new Entry("StoreName",systemFormat.getStoreName()));
+                entries.add(new Entry("CashRegisterNo",systemFormat.getCashRegisterNo()));
+                entries.add(new Entry("TimeStampDetails",systemFormat.getTimeStampDetails()));
+                entries.add(new Entry("StaffID",systemFormat.getStaffID()));
+                entries.add(new Entry("StaffName",systemFormat.getStaffName()));
+                entries.add(new Entry("PayFlag",systemFormat.getPayFlag()));
+                entries.add(new Entry("OrderStatus",orderStatus));
+                entries.add(new Entry("OrderSnWx",systemFormat.getOrderSnWx()));
+                entries.add(new Entry("OrderSnAli",systemFormat.getOrderSnAli()));
+                entries.add(new Entry("HSCode",systemFormat.getHsCode()));
+                entries.add(new Entry("HSCodeName",systemFormat.getHsCodeName()));
+                entries.add(new Entry("EmatouCode",systemFormat.getEmatouCode()));
+                entries.add(new Entry("PLU",systemFormat.getPlu()));
+                entries.add(new Entry("MychemID",systemFormat.getMychemID()));
+                entries.add(new Entry("ProductNameEN",systemFormat.getProductNameEN()));
+                entries.add(new Entry("ProductNameCN",systemFormat.getProductNameCN()));
+                entries.add(new Entry("Barcode",systemFormat.getBarcode()));
+                entries.add(new Entry("PackSize",systemFormat.getPackSize()));
+                entries.add(new Entry("ProductSpecification",systemFormat.getProductSpecification()));
+                entries.add(new Entry("Brand",systemFormat.getBrand()));
+                entries.add(new Entry("EDLP",systemFormat.getEdlp()));
+                entries.add(new Entry("CurrentPrice",systemFormat.getCurrentPrice()));
+                entries.add(new Entry("CostPrice",systemFormat.getCostPrice()));
+//                entries.add(new Entry("PrimeCost",systemFormat.getPrimeCost()));
+//                entries.add(new Entry("GoodsNumber",systemFormat.getGoodsNumber()));
+                entries.add(new Entry("DeductionRate",systemFormat.getDeductionRate()));
+                entries.add(new Entry("UnitSold",systemFormat.getUnitSold()));
+                entries.add(new Entry("Sales",systemFormat.getSales()));
+                entries.add(new Entry("TaxAmount",systemFormat.getTaxAmount()));
+                entries.add(new Entry("TotalSalesInclTax",systemFormat.getTotalSalesInclTax()));
+                entries.add(new Entry("TaxRate",systemFormat.getTaxRate()));
+                entries.add(new Entry("GP1",systemFormat.getGp1()));
+                entries.add(new Entry("GP2",systemFormat.getGp2()));
+                entries.add(new Entry("ProductCategory",systemFormat.getProductCategory()));
+                entries.add(new Entry("SupplierName",systemFormat.getSupplierName()));
+                entries.add(new Entry("TransactionType",systemFormat.getTransactionType()));
+                entries.add(new Entry("SaleReturnType",systemFormat.getSaleReturnType()));
+
+
+                entries.add(new Entry("Remark",systemFormat.getRemark()));
+
+                list.add(entries);
             }
 
         }
 
         // =================================================System Format导出结束=====================================
 
-        ee.addSheetByMap("Daily sales report", list, header);
+        ee.addSheetByList("Daily sales report", list, header);
         ee.export(response);
 
 
         return R.ok();
     }
 
-    private LinkedHashMap<String, Object> initHeaderMap() {
-        LinkedHashMap<String, Object> headerMap = new LinkedHashMap<>();
-        headerMap.put("ReceiptNo","销售单号");
-        headerMap.put("StoreName","门店名称");
-        headerMap.put("CashRegisterNo","收银台");
-        headerMap.put("TimeStampDetails","销售时间");
-        headerMap.put("StaffID","收银员");
-        headerMap.put("StaffName","收银员姓名");
-        headerMap.put("PayFlag","支付方式");
-        headerMap.put("OrderStatus","支付状态");
-        headerMap.put("OrderSnWx","微信流水号");
-        headerMap.put("OrderSnAli","支付宝流水号");
-        headerMap.put("HSCode","海关商品编码");
-        headerMap.put("HSCodeName","品类名称");
-        headerMap.put("EmatouCode","料件号");
-        headerMap.put("PLU","PLU");
-        headerMap.put("MychemID","MychemID");
-        headerMap.put("ProductNameEN","商品名称(英文)");
-        headerMap.put("ProductNameCN","商品名称(中文)");
-        headerMap.put("Barcode","商品主条码");
-        headerMap.put("PackSize","规格");
-        headerMap.put("ProductSpecification","单位");
-        headerMap.put("Brand","品牌");
-        headerMap.put("EDLP","日常价");
-        headerMap.put("CurrentPrice","实际销售价");
-        headerMap.put("CostPrice","进货价");
-//        headerMap.put("PrimeCost","成本价");
-//        headerMap.put("GoodsNumber","可用库存数");
-        headerMap.put("DeductionRate","扣率 (EDLP vs 实际销售价)");
-        headerMap.put("UnitSold","销售数量");
-        headerMap.put("Sales","销售额");
-        headerMap.put("TaxAmount","综合税额");
-        headerMap.put("TotalSalesInclTax","总销售额");
-        headerMap.put("TaxRate","综合税率");
-        headerMap.put("GP1","预估毛利额");
-        headerMap.put("GP2","预估毛利率");
-        headerMap.put("ProductCategory","商品类型");
-        headerMap.put("SupplierName","主供应商名称");
-        headerMap.put("TransactionType","销售类型");
-        headerMap.put("SaleReturnType","退货类型");
-        headerMap.put("Remark","备注");
-        return headerMap;
+    private ArrayList<Entry> initHeaderMap() {
+//        LinkedHashMap<String, Object> headerMap = new LinkedHashMap<>();
+        ArrayList<Entry> entries = new ArrayList<>();
+        entries.add(new Entry("ReceiptNo","销售单号"));
+        entries.add(new Entry("StoreName","门店名称"));
+        entries.add(new Entry("CashRegisterNo","收银台"));
+        entries.add(new Entry("TimeStampDetails","销售时间"));
+        entries.add(new Entry("StaffID","收银员"));
+        entries.add(new Entry("StaffName","收银员姓名"));
+        entries.add(new Entry("PayFlag","支付方式"));
+        entries.add(new Entry("OrderStatus","支付状态"));
+        entries.add(new Entry("OrderSnWx","微信流水号"));
+        entries.add(new Entry("OrderSnAli","支付宝流水号"));
+        entries.add(new Entry("HSCode","海关商品编码"));
+        entries.add(new Entry("HSCodeName","品类名称"));
+        entries.add(new Entry("EmatouCode","料件号"));
+        entries.add(new Entry("PLU","PLU"));
+        entries.add(new Entry("MychemID","MychemID"));
+        entries.add(new Entry("ProductNameEN","商品名称(英文)"));
+        entries.add(new Entry("ProductNameCN","商品名称(中文)"));
+        entries.add(new Entry("Barcode","商品主条码"));
+        entries.add(new Entry("PackSize","规格"));
+        entries.add(new Entry("ProductSpecification","单位"));
+        entries.add(new Entry("Brand","品牌"));
+        entries.add(new Entry("EDLP","日常价"));
+        entries.add(new Entry("CurrentPrice","实际销售价"));
+        entries.add(new Entry("CostPrice","进货价"));
+//        entries.add(new Entry("PrimeCost","成本价");
+//        entries.add(new Entry("GoodsNumber","可用库存数");
+        entries.add(new Entry("DeductionRate","扣率 (EDLP vs 实际销售价)"));
+        entries.add(new Entry("UnitSold","销售数量"));
+        entries.add(new Entry("Sales","销售额"));
+        entries.add(new Entry("TaxAmount","综合税额"));
+        entries.add(new Entry("TotalSalesInclTax","总销售额"));
+        entries.add(new Entry("TaxRate","综合税率"));
+        entries.add(new Entry("GP1","预估毛利额"));
+        entries.add(new Entry("GP2","预估毛利率"));
+        entries.add(new Entry("ProductCategory","商品类型"));
+        entries.add(new Entry("SupplierName","主供应商名称"));
+        entries.add(new Entry("TransactionType","销售类型"));
+        entries.add(new Entry("SaleReturnType","退货类型"));
+        entries.add(new Entry("Remark","备注"));
+        return entries;
     }
 
 }

+ 22 - 19
kmall-admin/src/main/webapp/js/common.js

@@ -313,27 +313,30 @@ function getQueryString(name) {
  * @param extraObj 往后台请求额外参数,对象格式,如:{'aaa': 111}
  */
 function exportFile(formId, url, extraObj) {
-    var form = $('<form>'); //定义一个form表单
-    form.attr('style', 'display: none');
-    form.attr('target', '');
-    form.attr('method', 'post');
-    form.attr('action', url);
-
-    var json = getJson(formId);
-    if (typeof extraObj != 'undefined') {
-        json = $.extend(json, extraObj);
-    }
+    window.confirm('确认要导出数据吗?', function () {
+        var form = $('<form>'); //定义一个form表单
+        form.attr('style', 'display: none');
+        form.attr('target', '');
+        form.attr('method', 'post');
+        form.attr('action', url);
+
+        var json = getJson(formId);
+        if (typeof extraObj != 'undefined') {
+            json = $.extend(json, extraObj);
+        }
 
-    $('body').append(form);//将表单放置在web中
-    for (var i in json) {
-        var input = $('<input>');
-        input.attr('type', 'hidden');
-        input.attr('name', i);
-        input.attr('value', json[i]);
-        form.append(input);
-    }
+        $('body').append(form);//将表单放置在web中
+        for (var i in json) {
+            var input = $('<input>');
+            input.attr('type', 'hidden');
+            input.attr('name', i);
+            input.attr('value', json[i]);
+            form.append(input);
+        }
 
-    form.submit();//表单提交
+        form.submit();//表单提交
+        window.alert('操作成功,正在导出,请勿重复点击!');
+    });
 }
 
 /**

+ 1 - 3
kmall-admin/src/main/webapp/js/compared/ordercomparederror.js

@@ -179,9 +179,7 @@ let vm = new Vue({
 				confirm('确认导出异常账单吗?', function () {
 					setTimeout(msg, 1000);
 					exportFile('#rrapp', '../ordercomparederror/exportDataFormat', params);
-					alert('操作成功,正在导出,请勿重复点击!', function (index) {
-						$("#jqGrid").trigger("reloadGrid");
-					});
+					$("#jqGrid").trigger("reloadGrid");
 				});
 				vm.flag = true
 			}else {

+ 4 - 8
kmall-admin/src/main/webapp/js/shop/mallpaymentorderdata.js

@@ -325,13 +325,9 @@ let vm = new Vue({
 					content: 'Loading...',
 					duration: 0
 				});
-				confirm('确认导出账单吗?', function () {
-					setTimeout(msg, 1000);
-					exportFile('#rrapp', '../mallpaymentorderdata/exportDataFormat', params);
-					alert('操作成功,正在导出,请勿重复点击!', function (index) {
-						$("#jqGrid").trigger("reloadGrid");
-					});
-				});
+				setTimeout(msg, 1000);
+				exportFile('#rrapp', '../mallpaymentorderdata/exportDataFormat', params);
+				$("#jqGrid").trigger("reloadGrid");
 				vm.flag = true
 			}else {
 				alert("点击的太快了,请慢一点!");
@@ -345,4 +341,4 @@ let vm = new Vue({
 			vm.machineCodeTypes = r.list;
 		});
 	},
-});
+});

+ 2 - 5
kmall-admin/src/main/webapp/js/shop/offilineOrderList.js

@@ -557,11 +557,8 @@ let vm = new Vue({
                     content: 'Loading...',
                     duration: 0
                 });
-                confirm('确认导出吗?', function () {
-                    setTimeout(msg, 1000);
-                    exportFile('#rrapp', '../order/exportSystemFormat', params);
-                    alert('操作成功,正在导出,请勿重复点击!');
-                });
+                setTimeout(msg, 1000);
+                exportFile('#rrapp', '../order/exportSystemFormat', params);
                 vm.flag = true;
             } else {
                 vm.flag = true;

+ 35 - 0
kmall-common/src/main/java/com/kmall/common/utils/excel/Entry.java

@@ -0,0 +1,35 @@
+package com.kmall.common.utils.excel;
+
+/**
+ * 替换掉导出Excel时的Map
+ * @author lhm
+ * @version 1.0
+ * 2021-07-02 16:31
+ */
+public class Entry {
+
+    private String key;
+
+    private String value;
+
+    public Entry(String key, String value) {
+        this.key = key;
+        this.value = value;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}

+ 41 - 0
kmall-common/src/main/java/com/kmall/common/utils/excel/ExcelExport.java

@@ -40,6 +40,7 @@ import java.net.URLEncoder;
 import java.sql.Timestamp;
 import java.text.DecimalFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 导出EXCEL功能包主类
@@ -256,6 +257,46 @@ public class ExcelExport {
 
     }
 
+    /**
+     * 主要功能: 在EXCEL中添加一个Sheet
+     * 注意事项:添加Sheet 并将List<Object[]> 中的数据填充
+     *
+     * @param sheetName  表单名字
+     * @param list       要填充的数据  以Object[] 表示单条记录
+     * @param colCaption 要生成的表头
+     */
+    public void addSheetByList(String sheetName, List<List<Entry>> list,
+                                String[] colCaption) {
+
+        // 创建表单
+        Sheet sheet;
+
+        if (StringUtils.isNullOrEmpty(sheetName)) {
+            sheet = workBook.createSheet();
+        } else {
+            sheet = workBook.createSheet(sheetName);
+        }
+
+        // 生成标题行 (表头)
+        createCaptionRow(colCaption, sheet);
+        // 记录列数
+        int colNum = 0;
+        // 从第二行开始
+        int startRow = 1;
+        CellStyle cellStyle = workBook.createCellStyle();
+
+        for (List<Entry> objects : list) {
+            List<Object> values = objects.stream().map(Entry::getValue).collect(Collectors.toList());
+            Row row = sheet.createRow(startRow++);
+            int cols = createRowData(row, values, cellStyle);
+            row.setHeight((short) 400);
+            colNum = colNum > cols ? colNum : cols;
+        }
+
+        adjustCellWidth(colNum, startRow, sheet);
+
+    }
+
 
     /**
      * 主要功能:在EXCEL中添加一个Sheet