Files

97 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2026-01-15 14:24:02 -06:00
"use client";
import { useState } from "react";
2026-02-13 16:10:05 -06:00
import { saveAs } from "file-saver";
import ExcelJS from "exceljs";
import styles from "./style.module.css";
interface Recibo {
id_recibo: number;
folio_recibo: string;
fecha_recibo: string;
fecha_registro: string;
monto: string;
user: {
usuario: string;
};
}
interface Columns{
header:string;
key:string;
width:number
}
interface Props {
data: Recibo[];
columns:Columns[];
}
2026-01-15 14:24:02 -06:00
2026-02-13 16:10:05 -06:00
export default function DownloadReporteXLSX({ data,columns }: Props) {
2026-01-15 14:24:02 -06:00
const [loading, setLoading] = useState(false);
2026-02-13 16:10:05 -06:00
const formatearFechaMX = (fechaISO: string) => {
return new Date(fechaISO).toLocaleString("es-MX", {
timeZone: "America/Mexico_City",
});
};
2026-01-15 14:24:02 -06:00
const handleDownload = async () => {
2026-02-13 16:10:05 -06:00
if (!data || data.length === 0) return;
2026-01-15 14:24:02 -06:00
setLoading(true);
try {
2026-02-13 16:10:05 -06:00
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("Recibos");
worksheet.columns = columns;
data.forEach((recibo) => {
worksheet.addRow({
folio: recibo.folio_recibo,
monto: Number(recibo.monto),
fechaRecibo: recibo.fecha_recibo,
fechaRegistro: formatearFechaMX(recibo.fecha_registro),
usuario: recibo.user.usuario,
});
});
worksheet.getColumn("monto").numFmt = '"$"#,##0.00';
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
saveAs(blob, "Reporte_Recibos.xlsx");
2026-01-15 14:24:02 -06:00
} catch (error) {
2026-02-13 16:10:05 -06:00
console.error("Error generando Excel", error);
2026-01-15 14:24:02 -06:00
} finally {
setLoading(false);
}
};
return (
<button
className={styles.downloadBtn}
onClick={handleDownload}
disabled={loading}
>
{loading ? (
<div className={styles.loader}></div>
) : (
2026-02-13 16:10:05 -06:00
<img
src="/excel.svg"
alt="Excel"
className={styles.iconExcel}
width={30}
height={30}
/>
2026-01-15 14:24:02 -06:00
)}
</button>
);
}