excel.js
/*
 * @Author: sunqianqian
 * @Date: 2023-12-20 09:25:22
 * @Description:下载excel文件的js
 */
import * as XLSX from 'xlsx';
function autoWidthFunc(ws, data) {
  const colWidth = data.map((row) =>
    row.map((val) => {
      var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g'); //检测字符串是否包含汉字
      if (val === null || val === undefined) {
        return { wch: 10 };
      } else if (reg.test(val)) {
        return { wch: val.toString().length * 2 };
      }
      return { wch: val.toString().length };
    })
  );
  // start in the first row
  const result = colWidth[0];
  for (let i = 1; i < colWidth.length; i++) {
    for (let j = 0; j < colWidth[i].length; j++) {
      if (result[j].wch < colWidth[i][j].wch) {
        result[j].wch = colWidth[i][j].wch;
      }
    }
  }
  ws['!cols'] = result;
}
function jsonToArray(key, jsonData) {
  return jsonData.map((v) =>
    key.map((j) => {
      return v[j];
    })
  );
}
const exportArrayToExcel = ({
  key,
  data,
  title,
  filename,
  autoWidth,
  toptitle,
  condition,
  statistic,
  statisticount,
}) => {
  const wb = XLSX.utils.book_new();
  const arr = jsonToArray(key, data);
  arr.unshift(title);
  if (statisticount) {
    arr.unshift(['', '', '']);
    arr.unshift(statisticount);
  }
  if (statistic) {
    arr.unshift(statistic);
  }
  if (condition) {
    arr.unshift(['', '', '']);
    arr.unshift(condition);
  }
  if (toptitle) {
    arr.unshift(['', '', '']);
    arr.unshift(toptitle);
  }
  const ws = XLSX.utils.aoa_to_sheet(arr);
  if (autoWidth) {
    autoWidthFunc(ws, arr);
  }
  XLSX.utils.book_append_sheet(wb, ws, filename);
  XLSX.writeFile(wb, filename + '.xlsx');
};
export default {
  exportArrayToExcel,
};
引用
只有一组数据时
import excel from '@/utils/downloadExcel.js';
// 导出  
    exportExcel() {
      const params = {
        title: ['名字', '数量'],
        key: ['name', 'count'],
        data: this.groupItemList[0].children, // 数据源
        autoWidth: true, //autoWidth等于true,那么列的宽度会适应那一列最长的值
        filename: this.groupItemList[0].title,
      };
      excel.exportArrayToExcel(params);
    },
多种数据时
exportExcel() {
      const params = {
        // toptitle: ['引证报告', '', ''],
        condition: ['检索条件:', this.citationSearchTerm, ''],
        statistic: ['发文量', '被引频次', '篇均被引频次'],
        statisticount: [
          this.statisticlist[0].num,
          this.statisticlist[1].num,
          this.statisticlist[2].num,
        ],
        title: ['引证文献列表'],
        key: ['str'],
        data: this.exportListData, // 数据源
        autoWidth: true, //autoWidth等于true,那么列的宽度会适应那一列最长的值
        filename: '引证报告',
      };
      excel.exportArrayToExcel(params);
    },