浏览代码

no message

lvhao 6 月之前
父节点
当前提交
5bb1e36859
共有 1 个文件被更改,包括 68 次插入1 次删除
  1. 68 1
      template/erp/returngoods.html

+ 68 - 1
template/erp/returngoods.html

@@ -295,7 +295,11 @@ function excelexport(){
                 if (a.data.length > 0){
                     $(".ts p").html(a.msg);
                     $(".ts").fadeIn();
-                    explortExcel(a.data,"退货单列表.xlsx")
+                    const now = new Date();
+                    const dateString = now.toISOString().split('T')[0];
+                    const [year, month, day] = dateString.split('-');
+                    let filename = "售后登记清单"+year+month+day+".xlsx";
+                    explortExcel(a.data,filename)
                 }else{
                     $(".ts p").html("没有数据");
                     $(".ts").fadeIn();
@@ -306,10 +310,73 @@ function excelexport(){
             }
    },'json')
 }
+function getCellWidth(value) {
+  // 判断是否为null或undefined
+  if (value == null) {
+    return 10;
+  } else if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
+    // 中文的长度
+    const chineseLength = value.match(/[\u4e00-\u9fa5]/g).length;
+    // 其他不是中文的长度
+    const otherLength = value.length - chineseLength;
+    return chineseLength * 2.1 + otherLength * 1.1;
+  } else {
+    return value.toString().length * 1.1;
+    /* 另一种方案
+    value = value.toString()
+    return value.replace(/[\u0391-\uFFE5]/g, 'aa').length
+    */
+  }
+}
+
 function explortExcel(data,filename){
     
+
+
     let  sheet = XLSX.utils.json_to_sheet(data)
+
+    let colWidths = [],
+    colNames = Object.keys(data[0]) // 所有列的名称数组
+
+    // 计算每一列的所有单元格宽度
+    // 先遍历行
+    data.forEach((row) => {
+    // 列序号
+    let index = 0
+    // 遍历列
+    for (const key in row) {
+        if (colWidths[index] == null) colWidths[index] = []
+
+            switch (typeof row[key]) {
+            case 'string':
+            case 'number':
+            case 'boolean':
+                colWidths[index].push(getCellWidth(row[key]))
+                break
+            case 'object':
+            case 'function':
+                colWidths[index].push(0)
+                break
+            }
+            index++
+        }
+    })
+
+    sheet['!cols'] = []
+    // 每一列取最大值最为列宽
+    colWidths.forEach((widths, index) => {
+        // 计算列头的宽度
+        widths.push(getCellWidth(colNames[index]))
+        // 设置最大值为列宽
+        console.log(Math.max(...widths))
+        sheet['!cols'].push({ wch: Math.max(...widths) })
+    })
+
+
+
+
     let workbook = XLSX.utils.book_new();
+    
     XLSX.utils.book_append_sheet(workbook, sheet, "Sheet1");
     XLSX.writeFile(workbook, filename);
 }