Read and write data in xlxs using node js
Here is our Excel file containing fields:
Let's start,
npm init -y
const express = require("express");
var Excel = require("exceljs");
const app = express();
var wb = new Excel.Workbook();
app.get("/readxlxs", (req, res) => {
const dda = await wb.xlsx
.readFile("./demo.xlsx")
.then(async function () {
var sh = wb.getWorksheet("Sheet1");
for (let j = 1; j <= sh.rowCount; j++) {
for (i = 2; i <= sh.columnCount; i++) {
console.log("values == ",sh.getRow(j).getCell(i).value);
}
}
});
})
Step 5: For writing data to an excel file, go with the below mention code.
app.get("/writexlxs", (req, res) => {
const dda = await wb.xlsx
.readFile("./demo.xlsx")
.then(async function () {
var sh = wb.getWorksheet("Sheet1");
sh.getRow(2).getCell(1).value="Rohit"
wb.xlsx.writeFile("demo.xlsx");
});
})
Step 6: Please mention the port number for running it on the local server.
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server is running on port ${port}.`);
});
Amazing post
ReplyDeleteIts great blog. It's helpful for node js aspirants. Keep it up.
ReplyDeleteGood
ReplyDelete