I want to click the bottom and download the excel file, but I can't figure out why it is not working.
the main problem is at tRPC router side.
the tool I using:
tRPC router:
.mutation("xlsx", {
input: z.object({
id: z.string(),
}),
resolve: async ({ ctx }) => {
const FILE_PATH = "./src/utils/01.xlsx";
const wb = new ExcelJs.Workbook();
await wb.xlsx.readFile(FILE_PATH).then(() => {
var ws = wb.getWorksheet(1);
ws.getCell("H4").value = "fkfk";
});
return wb.xlsx.write(ctx.res);
},
});
Frontend:
function Print() {
const xlsxMutation = trpc.useMutation(['guest.xlsx'])
const onDownload = React.useCallback(()=>{
xlsxMutation.mutate({
id:"test"
})
},[xlsxMutation])
return (
<>
<button onClick={()=>handleClickOpen()}>download</button>
</>
);
}
the codesandbox not install ExcelJS yet, because I'm not sure why the error show up.
anyway, it simulated my code structure.
is there anyone using NextJS tRPC and ExcelJS share the code.
##edit
since the xlsx file already exist (FILE_PATH), I should something like ctx.res.pipe()
right? but how??