Skip to content
background-image background-image

Using ExcelJS

Node.JS module called ExcelJS and export it for Node.JS statement as exceljs

NodeJS statement produces valid Excel (xlsx format)

const input = [
    {
        "id": 1,
        "name": "John Doe",
        "email": "john.doe@example.com",
        "age": 30,
        "is_active": true,
        "roles": ["admin", "user"]
    },
];

// Create a new workbook and add a worksheet
const workbook = new exceljs.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');

// Add column headers
worksheet.columns = [
    { header: 'ID', key: 'id', width: 10 },
    { header: 'Name', key: 'name', width: 30 },
    { header: 'Email', key: 'email', width: 30 },
    { header: 'Age', key: 'age', width: 10 },
    { header: 'Active', key: 'is_active', width: 10 },
    { header: 'Roles', key: 'roles', width: 30 }
];

// Add rows from JSON data
input.forEach(item => {
    worksheet.addRow({
        id: item.id,
        name: item.name,
        email: item.email,
        age: item.age,
        is_active: item.is_active,
        roles: item.roles.join(', ')
    });
});

// Write to a buffer and convert to Base64
const buffer = await workbook.xlsx.writeBuffer();
const data = buffer.toString('base64');

return [
    {
        Data: data
    }
]

See ExcelJS documentation for more examples.