import { saveAs } from 'file-saver';
import * as XLSX from 'xlsx';

export const exportSheet = (rows = [], sheetName = 'Data', fileName = 'Data.xlsx') => {
  const ws = XLSX.utils.json_to_sheet(rows);
  const wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, sheetName);

  const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });

  const excelBlob = new Blob([excelBuffer], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  });

  return saveAs(excelBlob, fileName);
};
