import * as XLSX from 'xlsx';

const importsExcel=(event)=> {
  return new Promise((resolve, reject)=>{
    const file = event.target.files[0];
    const reader = new FileReader();
    console.log('file',file)
    reader.onload = (e) => {
      try {
        const data = e.target.result;

        console.log('data',data)
        const workbook = XLSX.read(data, { type: "binary" });

        // this.setState({
        //   excelData: workbook.Sheets.Sheet1.data,
        // });
        console.log('workbook',workbook)
        const res = XLSX.utils.sheet_to_json(workbook.Sheets.sheet1)
        resolve(res)
        console.log('res',res)
      }catch (e) {
        // 这里可以抛出文件类型错误不正确的相关提示
        reject("失败");// 导出失败
      }
    };
    reader.readAsBinaryString(file);
  })
}

const antdImportExcel = (event)=>{
  return new Promise((resolve, reject)=>{
    const file = event.file.originFileObj;
    const reader = new FileReader();
    console.log('file',file)
    reader.onload = (e) => {
      try {
        const data = e.target.result;

        console.log('data',data)
        const workbook = XLSX.read(data, { type: "binary" });

        // this.setState({
        //   excelData: workbook.Sheets.Sheet1.data,
        // });
        console.log('workbook',workbook)
        const res = XLSX.utils.sheet_to_json(workbook.Sheets.sheet1)
        resolve(res)
        console.log('res',res)
      }catch (e) {
        // 这里可以抛出文件类型错误不正确的相关提示
        reject("失败");// 导出失败
      }
    };
    reader.readAsBinaryString(file);
  })
}

export {
  importsExcel,
  antdImportExcel
}
