import ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';

const exportDataToExcel = async (data: any, fileName: any) => {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Sheet1');
    worksheet.columns = Object.keys(data[0]).map((key) => {
        return { header: key, key: key, width: 20 }
    })
    data.forEach((item: any) => {
        worksheet.addRow(item);
    });
    const buffer = await workbook.xlsx.writeBuffer();
    saveAs(new Blob([buffer]), `${fileName}.xlsx`);
};

export default exportDataToExcel;
