vue3
<!--  -->
<template>
  <p>点位</p>
  <input type="file" @change="readFile" accept=".xlsx" />
</template>

<script setup>
import {
  reactive,
  ref,
  toRefs,
  onBeforeMount,
  onMounted,
  nextTick,
  watch,
} from "vue";
import * as XLSX from "xlsx";
const readFile = (e) => {
  const files = e.target.files;
  // 如果没有文件名
  if (files.length <= 0) {
    return false;
  } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
    this.$Message.error("上传格式不正确，请上传xls或者xlsx格式");
    return false;
  }

  const fileReader = new FileReader();
  fileReader.onload = (ev) => {
    try {
      const data = ev.target.result;
      // 切换为新的调用方式
      const workbook = XLSX.read(data, {
        type: "binary",
      });

      let table = workbook.Sheets;
      let keys = workbook.SheetNames;
      console.log(workbook);
      let array = [];

      keys.forEach((item) => {
        let obj = {};
        obj.name = item;
        // obj.list = XLSX.utils.sheet_to_json(table[item]);
        if (item == "楼盘信息") {
          obj.list = procressData(
            XLSX.utils.sheet_to_json(table[item], {
              /** Default value for null/undefined values */
              defval: "", //给defval赋值为空的字符串
            })
          );
          array.push(obj);
        }
      });
    } catch (e) {
      return false;
    }
  };
  fileReader.readAsBinaryString(files[0]);
};
// 表格时间转换
function excelToJsDate(serialNumber) {
  var date = new Date(1900, 0, serialNumber - 1);

  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();

  return year + "年" + month + "月" + day + "日";
}
const filterName = (name) => {
  if (!name) return "";
  console.log(name.split("."));
  return name.split(".")[1] || name.split(".")[0];
};
const procressData = (list) => {
  let array = [];
  console.error(list);
  // list = list.filter((item) => item["提供日期"]);

  list.forEach((item, index) => {
    let obj = {};
    // obj.id = index + 1;
    for (let key in item) {
      switch (key) {
        case "楼盘名称":
          obj["title"] = item[key];

          break;
        case "楼盘ID":
          obj["id"] = item[key];

          break;
        case "VR":
          obj["images_1"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "效果图":
          obj["images_2"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "样板间":
          obj["images_3"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "区位":
          obj["images_4"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "小区配套":
          obj["images_5"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "VR链接":
          if (item[key]) {
            obj["vrLink"] = item[key];
          }

          break;

        case "标签":
          obj["tags"] = item[key].replaceAll("，", ",").split(",");
          break;
        case "特点":
          obj["label"] = item[key].replaceAll("，", ",").split(",");
          break;

        case "楼盘户型":
          obj["cad"] = item[key];

          break;
        case "最新开盘":
          let time =
            new Date(item[key]) * 24 * 60 * 60 * 1000 + Date.UTC(1899, 11, 30);
          const formattedDate = formatDate(time);
          obj["time"] = formattedDate;

          break;
        case "项目地址":
          obj["address"] = item[key];
          break;
        case "新房顾问":
          obj["agent"] = item[key];
          break;
        case "均价":
          obj["price"] = item[key];
          break;
        case "总价":
          obj["total"] = item[key];
          break;
        case "谷歌经纬度":
          obj["location"] = item[key];
          break;
      }
    }
    // obj.vrLink = "https://www.4dkankan.com/panorama/show.html?id=WK1768236336482967552&vr=fd720_NZ0i3ASIq&lang=zh"
    array.push(obj);
  });

  console.error(array);
  return array;
};
// 格式化日期
const formatDate = (timestamp) => {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day}`;
};

// 使用函数格式化日期

onMounted(() => {});
</script>
<style lang="scss" scoped></style>
<style></style>
