lsc hace 3 semanas
padre
commit
06d2b1e528
Se han modificado 2 ficheros con 12 adiciones y 5 borrados
  1. 8 3
      src/common/Export2Excel.js
  2. 4 2
      src/components/pages/studentManage/student.vue

+ 8 - 3
src/common/Export2Excel.js

@@ -150,10 +150,8 @@ export function export_table_to_excel(id) {
 function formatJson(jsonData) {
   console.log(jsonData)
 }
-export function export_json_to_excel(th, jsonData, defaultTitle) {
-
+export function export_json_to_excel(th, jsonData, defaultTitle, colWidths) {
   /* original data */
-
   var data = jsonData;
   data.unshift(th);
   var ws_name = "SheetJS";
@@ -161,6 +159,13 @@ export function export_json_to_excel(th, jsonData, defaultTitle) {
   var wb = new Workbook(),
     ws = sheet_from_array_of_arrays(data);
 
+  // 设置列宽
+  if (colWidths && ws) {
+    ws['!cols'] = colWidths.map(function(width) {
+      // colWidths为每列对应的宽度数字,单位为字符数
+      return { wch: width };
+    });
+  }
 
   /* add worksheet to workbook */
   wb.SheetNames.push(ws_name);

+ 4 - 2
src/components/pages/studentManage/student.vue

@@ -352,9 +352,11 @@ export default {
     getExcel(res) {
       require.ensure([], () => {
         const { export_json_to_excel } = require("../../../common/Export2Excel");
-        const tHeader = ["学生姓名", "学生账号(建议直接使用学号)", "班级"];//"学号", "学生手机号", 
+        // 增加 colWidths 参数以调整列宽,例如调整为 [20, 35, 20]
+        const tHeader = ["学生姓名", "学生账号(建议直接使用学号)", "班级"];
         const data = [];
-        export_json_to_excel(tHeader, data, "上传学生样例");
+        const colWidths = [20, 35, 20]; // 设置每一列的宽度
+        export_json_to_excel(tHeader, data, "上传学生样例", colWidths);
       });
     },
     handleCurrentChange(val) {