简体   繁体   English

ExcelJS writeFile 不起作用:Uncaught TypeError: fs.createWriteStream is not a function

[英]ExcelJS writeFile not working: Uncaught TypeError: fs.createWriteStream is not a function

I'm trying to export a formatted excel file using ExcelJS and the writeFile method simply isn't working.我正在尝试使用 ExcelJS 导出格式化的 excel 文件,而 writeFile 方法根本不起作用。 I get the following exception when I call the function:调用函数时出现以下异常:

Uncaught TypeError: fs.createWriteStream is not a function

My javascript is bundled using browserify.js.我的 javascript 是使用 browserify.js 捆绑的。 Here's my source code:这是我的源代码:

index.html索引.html

<!DOCTYPE html>
<head>
  <title>Test Excel JS</title>
  <meta charset="utf-8">
  <meta name="description" content="">

  <link rel="stylesheet" href="app.css">
</head>
<body>
  <div>
    <label>Test</label>
    <button onclick="test()">Test this Stuff and Check your console log</button>
  </div>

  <script src="bundle.js"></script>
  <script>
      var test = function(){
          writeFile();
      };
  </script>
</body>
</html>

app.js (bundled by browserify into bundle.js) app.js(由 browserify 捆绑成 bundle.js)

'use strict';

global.Excel = require('../node_modules/exceljs/dist/es5/exceljs.browser');

global.isRowBold = function(excelRow){
    return excelRow.getCell('name').value === "Jeff";
};

global.getRowColor = function(excelRow){
    return excelRow.getCell('color').value;
};

global.getCellColor = function(excelRow, cell){
    return (excelRow.getCell('name').value === 'John' && cell.value === 0)? 'orange' : excelRow.getCell('color').value;
};

global.getFont = function(isBold, color){
    return {
        name: 'Arial Black',
        color: color,
        family: 2,
        size: 14,
        bold: isBold
    };
};

global.getTestHeader = function(){
    return [
        {key: "id", header: "Id"},
        {key: "name", header: "Name", width: 32},
        {key: "color", header: "Color", width: 10}
    ];
};

global.getTestData = function(){
    return [
        {
            id: 0,
            name: "John",
            color: "green"
        },
        {
            id: 1,
            name: "Rehan",
            color: "blue"
        },
        {
            id: 2,
            name: "Jeff",
            color: "yellow"
        }
    ];
};

global.generateTestFile = function(){
    var workbook = new Excel.Workbook();

    workbook.creator = "Generated";
    workbook.lastModifiedBy = "Generated";
    workbook.created = new Date();
    workbook.modified = new Date();

    var worksheet = workbook.addWorksheet('Sheet 1');

    //Set Column Headers
    worksheet.columns = getTestHeader();

    //Add Rows
    var testData = getTestData();
    var length = testData.length;
    for(var i = 0; i < length; i++){
        worksheet.addRow(testData[i]);
    }

    //Format Rows
    worksheet.eachRow(function(row, rowNumber){
        var isBold = isRowBold(row);
        row.eachCell(function(cell, colNumber){
            var cellColor = getCellColor(row, cell);
            cell.font = getFont(isBold, cellColor);
        });
    });

    //workbook.commit();
    return workbook;
};

global.writeFile = function(){
    var workbook = generateTestFile();
    workbook.xlsx.writeFile('./output/newtestfile.xlsx')
        .then(function() {
            console.log('Done Writing file');
        });
};

fs.createWriteStream is a method meant to be used on Node.js (server). fs.createWriteStream是一种用于 Node.js(服务器)的方法。 These kinds of methods won't be available on the browser (client-side).这些方法在浏览器(客户端)上不可用。

change writeFile to writeBuffer将 writeFile 更改为 writeBuffer

use filesaver使用文件保护程序

workbook.xlsx.writeBuffer()
  .then(buffer => FileSaver.saveAs(new Blob([buffer]), `${Date.now()}_feedback.xlsx`))
  .catch(err => console.log('Error writing excel export', err)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:fs.​​createWriteStream不是函数 - Uncaught TypeError: fs.createWriteStream is not a function 类型错误:fs.createWriteStream 不是 Node 中的 function - TypeError: fs.createWriteStream is not a function in Node 错误TypeError:fs.​​createWriteStream不是函数Angular 6 GIF导出 - ERROR TypeError: fs.createWriteStream is not a function Angular 6 GIF export 未捕获的类型错误:fs.writeFile 不是 function - Uncaught TypeError: fs.writeFile is not a function Node.js fs.createWriteStream 未定义 function - Node.js fs.createWriteStream undefined function 将 Electron 与 React 一起使用时出现“未捕获的类型错误:fs.writeFile 不是函数” - “Uncaught TypeError: fs.writeFile is not a function” when using Electron with React 如何确保fs.createWriteStream在函数继续执行之前完成; 仅保存一部分图像 - How to make sure fs.createWriteStream finishes before function continues; only fraction of image being saved 解决在chrome中运行的tensorflow js解决“未捕获(承诺)TypeError:fs.​​writeFile不是函数”的另一种方法 - Alternative way to solve “Uncaught (in promise) TypeError: fs.writeFile is not a function” for tensorflow js running in chrome 带有fs.createWriteStream()的节点请求模块创建一个空文件 - Node request module with fs.createWriteStream() creates an empty file 错误事件上的fs.createWriteStream无法捕获错误 - fs.createWriteStream on error event does not catch errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM