const con = require("../lib/db");
const excel = require("exceljs");
const db_excel_post = (req, res, next) => {
con.query("SELECT * FROM db", (err, details, fields) => {
const jsonData = JSON.parse(JSON.stringify(details));
let workbook = new excel.Workbook(); // Creating workbook
let worksheet = workbook.addWorksheet("Profile"); // Creating worksheet
worksheet.columns = [
{ header: "Name", key: "name", width: 10 },
{ header: "Age", key: "age", width: 10 },
{ header: "Job", key: "job", width: 20 }
];
worksheet.addRows(jsonData);
workbook.xlsx
.writeFile("Profile.xlsx")
.then(() => console.log("File saved!"));
});
res.redirect("/");
};
module.exports = {
db_excel_post
};
I have tested the codes above and it is working fine. It has created "Profile.xlsx" in same directory of app.js (server).
Please see the picture (https://i.stack.imgur.com/izAnB.png)
Is it possible that the Profile.xlsx file can be saved/downloaded to the "public" folder? I have read through Exceljs documentation, it seems to be none of it talking about saving the .xlsx to other path. Appreciate your kind help. Thanks
CodePudding user response:
you just include the file path in the filename.
workbook.xlsx
.writeFile("./public/Profile.xlsx")
.then(() => console.log("File saved!"));
});