Angular : Can't export excel using ExcelJS - error TS2307: Cannot find module 'stream' - error TS2503: Cannot find namespace 'NodeJS'

前端 未结 2 1716
长情又很酷
长情又很酷 2021-01-21 08:23

I was try to export an excel file using ExcelJS

Here is my console in VS Code terminal :

ERROR in node_modules/exceljs/         


        
相关标签:
2条回答
  • 2021-01-21 08:52

    You can't write directly a file on client side. that method is mean to be used on backend side in nodejs. if your expectation is download this file on client side. your code should be like :-

    import { Component } from "@angular/core";
    import * as Excel from "exceljs";
    @Component({
      selector: "my-app",
      templateUrl: "./app.component.html",
      styleUrls: ["./app.component.css"]
    })
    export class AppComponent {
      title = "resttest10";
    
      async downloadExcel() {
        const date = new Date()
          .toISOString()
          .slice(0, 10)
          .split("-")
          .reverse()
          .join("/");
        console.log(date);
        const workbook = new Excel.Workbook();
        const worksheet = workbook.addWorksheet("My Sheet");
    
        worksheet.columns = [
          { header: "Id", key: "id", width: 10 },
          { header: "Name", key: "name", width: 32 },
          { header: "D.O.B.", key: "dob", width: 15 }
        ];
    
        worksheet.addRow({ id: 1, name: "John Doe", dob: new Date(1970, 1, 1) });
        worksheet.addRow({ id: 2, name: "Jane Doe", dob: new Date(1965, 1, 7) });
    
        workbook.xlsx.writeBuffer().then((data: any) => {
          console.log("buffer");
          const blob = new Blob([data], {
            type:
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          });
          let url = window.URL.createObjectURL(blob);
          let a = document.createElement("a");
          document.body.appendChild(a);
          a.setAttribute("style", "display: none");
          a.href = url;
          a.download = "export.xlsx";
          a.click();
          window.URL.revokeObjectURL(url);
          a.remove();
        });
      }
    }
    
    0 讨论(0)
  • 2021-01-21 09:07

    In your tsconfig.app.json file add "types": ["node"]

    0 讨论(0)
提交回复
热议问题