Page cover

Excel

1. Giới thiệu

2. Tạo file pdf

Sử dụng thư viện https://www.npmjs.com/package/xlsx

// Some code
app.use("/api/excel", (req, res) => {
  const fileName = "./public/output.xlsx";
  console.log(req.query);
  createExeclFile(fileName, req.query.name);
  res.redirect("http://127.0.0.1:3000" + fileName.substring(1));
});

module.exports = app;

async function createExeclFile(fileName, name) {
  if (typeof require !== "undefined") XLSX = require("xlsx");
  const fs = require("fs");
  fs.createWriteStream(fileName);
  var workbook = XLSX.readFile(fileName);
  /* output format determined by filename */

  /* at this point, out.xlsb is a file that you can distribute */
  var ws_name = "Sheet2";

  /* make worksheet */
  var ws_data = [
    ["S", "h", "e", "e", "t", "J", "S"],
    [1, 2, 3, 4, 5, name],
  ];
  var ws = XLSX.utils.aoa_to_sheet(ws_data);

  /* Add the worksheet to the workbook */
  XLSX.utils.book_append_sheet(workbook, ws, ws_name);
  XLSX.writeFile(workbook, fileName);
}

3. Dùng Stream

Last updated

Was this helpful?