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

export const exportToExcel = (data: any[], fileName: string = "data") => {
	if (!data || data.length === 0) return;

	const worksheet = XLSX.utils.json_to_sheet(data);

	const workbook = XLSX.utils.book_new();
	XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");

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

	const blob = new Blob([excelBuffer], {
		type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
	});

	saveAs(blob, `${fileName}.xlsx`);
};
