import * as xlsx from 'xlsx'
import { WorkBook } from 'xlsx/types'

export function exportExcelFile(
  array: any[],
  width?: number[],
  fileName = '展馆预约记录',
  sheetName = '表1'
) {
  // 设置列宽或默认150
  const columnWidths = width
    ? width.map((w, index) => ({ wpx: w }))
    : Array(Object.keys(array[0]).length).fill({ wpx: 150 })
  const jsonWorkSheet = xlsx.utils.json_to_sheet(array)
  jsonWorkSheet['!cols'] = columnWidths
  const workBook: WorkBook = {
    SheetNames: [sheetName],
    Sheets: {
      [sheetName]: jsonWorkSheet
    }
  }
  return xlsx.writeFile(workBook, `${fileName}.xlsx`)
}
