1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

ExcelJs/Filesaver.js exporting a read only file

Discussão em 'Angular' iniciado por Pramod Kumar, Setembro 10, 2024.

  1. Pramod Kumar

    Pramod Kumar Guest

    I'm trying to export json data to excel using ExcelJS library in angular project. I have followed all the steps present in the following link

    Export to Excel in Angular using ExcelJS

    It's working and I'm able to export the data. But the file is being exported as read only file. This is the code which i'm using in my project

    import { Injectable } from "@angular/core";
    import * as ExcelJS from 'exceljs';
    import { saveAs } from 'file-saver';

    @Injectable()
    export class ExportService{

    exportToExcel(data: any[], fileName: string) {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('My Sheet');

    // Add headers
    const headers = Object.keys(data[0]);
    worksheet.addRow(headers);

    // Add data
    data.forEach((item) => {
    const row:any = [];
    headers.forEach((header) => {
    row.push(item[header]);
    });
    worksheet.addRow(row);
    });

    worksheet.getColumn(1).width = 15;
    worksheet.getColumn(2).width = 20;


    // Generate Excel file
    workbook.xlsx.writeBuffer().then((buffer: any) => {
    const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    saveAs(blob, `${fileName}.xlsx`);
    });
    }
    }


    Is it an issue in format in File saver js or Excel js??

    Continue reading...

Compartilhe esta Página