【问题标题】:how to download the excel file on clicking a button using npm package exceljs如何使用 npm 包 exceljs 单击按钮下载 excel 文件
【发布时间】:2020-04-29 07:16:30
【问题描述】:

我在 nodejs 中创建了一个 get api,我正在使用第三方包 exceljs,get api 工作正常,当我在浏览器中调用 get api 为 http://localhost:3000/createExcel 时,它正在将文件下载为 excel 表但是我希望客户端在单击下载按钮时下载该文件,因此我创建了一个简单的 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>

【问题讨论】:

    标签: node.js vue.js exceljs


    【解决方案1】:

    ajax 请求永远不会自动为您保存文件。因此,在新窗口中打开 url http://localhost:3000/createExcel。 试试

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

    【讨论】:

      【解决方案2】:

      我推荐使用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') 经常被浏览器屏蔽。

      【讨论】:

        猜你喜欢
        • 2011-10-16
        • 1970-01-01
        • 1970-01-01
        • 2016-11-06
        • 2018-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多