如何使用 npm 包 exceljs 单击按钮下载 excel 文件

how to download the excel file on clicking a button using npm package exceljs

我在使用第三方包 exceljs 的 nodejs 中创建了一个 get api,get api 工作正常,当我调用 get api 在浏览器中作为 http://localhost:3000/createExcel 它正在将文件下载为 excel sheet 但我希望客户端在单击下载按钮时下载该文件所以我创建了一个简单的 html 文件,按钮为 Download Excel File,我正在使用 axios 在该按钮上调用 get api,但没有任何反应,它没有下载文件?

知道如何解决这个问题:-

我的服务器端代码:-

const MongoClient = require("mongodb");
const express = require("express");
const cors = require('cors');
const url = "mongodb://127.0.0.1:27017";
const Excel = require("exceljs");

const app = express();
app.use(cors);


MongoClient.connect(url, { useUnifiedTopology: true }, async (err, db) => {
  // Handle error
  if (err) {
    throw err;
  }
  let dbo = db.db("ronak");
  dbo
    .collection("excelData")
    .find({})
    .toArray((err, result) => {
      if (err) {
        throw err;
      }
      app.get("/createExcel", (req, res, next) => {
        var workbook = new Excel.Workbook();

        workbook.creator = "Me";
        workbook.lastModifiedBy = "Him";
        workbook.created = new Date(1985, 8, 30);
        workbook.modified = new Date();
        workbook.lastPrinted = new Date(2016, 9, 27);
        workbook.properties.date1904 = true;

        workbook.views = [
          {
            x: 0,
            y: 0,
            width: 10000,
            height: 20000,
            firstSheet: 0,
            activeTab: 1,
            visibility: "visible",
          },
        ];
        var worksheet = workbook.addWorksheet("Sales");
        worksheet.columns = [
          { header: "brand", key: "brand", width: 30 },
          { header: "emp_id", key: "emp_id", width: 10 },
          {
            header: "quarter_no_with_start_month",
            key: "quarter_no_with_start_month",
            width: 20,
          },
          { header: "target", key: "target", width: 20 },
          { header: "units", key: "units", width: 20 },
          { header: "value", key: "value", width: 20 },
        ];
        worksheet.eachRow({ includeEmpty: true }, function (row) {
          row.eachCell(function (cell, colNumber) {
            cell.font = {
              name: "Arial",
              family: 2,
              bold: false,
              size: 14,
            };
            cell.alignment = {
              vertical: "middle",
              horizontal: "center",
            };
          });
        });
        worksheet.addRows(result);

        res.setHeader(
          "Content-Type",
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        );
        res.setHeader(
          "Content-Disposition",
          "attachment; filename=" + "Report.xlsx"
        );
        workbook.xlsx.write(res).then((data) => {
          res.end();
          console.log("File write done", data);
        });
      });
      db.close();
    });
});


app.listen(3000, (err) => {
  if (err) {
    console.log("Error connecting to port 3000:", err);
  } else {
    console.log("Listening on port", 3000);
  }
});

这是我在 vuejs 中的 html 代码:-

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <!-- the star of the show - the Vue library! -->
  <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
  <script>
    // when life is settled, load up the fun stuff
    document.addEventListener('DOMContentLoaded', function () {
      new Vue({
        el: '#app',
        // define data - initial display text
        methods: {
            async downloadExcelFile() {
               const res = await axios.get('http://localhost:3000/createExcel')
                console.log(res);
            }
        }
      })
    })
  </script>
</head>
<body>
  <div id="app">
    <button v-on:click="downloadExcelFile">Download Excel File</button>
  </div>
</body>
</html>

ajax 请求永远不会自动为您保存文件。于是打开urlhttp://localhost:3000/createExcel就新建了window。 尝试

methods: {
        async downloadExcelFile() {
           window.open('http://localhost:3000/createExcel', '_blank');
        }
    }

我推荐使用file-saverhttps://www.npmjs.com/package/file-saver

npm install --save file-server 

js代码:

import { saveAs } from 'file-saver';

//....//

const blob = new Blob([res], {
  type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
saveAs(blob, "file.xlsx");

我认为它也可以用于使用:https://github.com/Siemienik/xlsx-renderer 它应该使文件生成更容易和更可配置,因为所有文件结构都在 Excel 中创建并用作模板。

编辑:据我所知,window.open(...,'_blank')经常被浏览器屏蔽。