import XLSX from 'xlsx'

/**
 * xlsx文件转换成json数据
 * @param {File} file xlsx文件
 * @returns {Promise<error:string, Array>}
 */
export function import_excel_to_json(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()

    reader.onload = function(e) {
      const data = e.target.result
      const wb = XLSX.read(data, { type: 'binary' })
      const jsonData = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
      resolve(jsonData)
    }
    reader.onerror = () => reject('错误的文件类型!')
    reader.readAsBinaryString(file)
  })
}
