Export Data Effortlessly with Excel Export A Comprehensive Guide

Introduction to Excel Export

Excel Export is a powerful tool for exporting data from applications into Excel files effortlessly. This tool comes with a variety of APIs for different use-cases. In this guide, we will explore some of the most useful APIs available and provide code snippets to demonstrate their use.

API Examples

1. Basic Export to Excel

This is a basic example of exporting data to an Excel file.

  const excelExport = require('excel-export');
  
  const conf = {};
  conf.cols = [
    { caption: 'Name', type: 'string' },
    { caption: 'Age', type: 'number' }
  ];
  conf.rows = [
    ['Alice', 25],
    ['Bob', 30]
  ];
  
  const result = excelExport.execute(conf);
  require('fs').writeFileSync('output.xlsx', result, 'binary');

2. Custom Formatting

Apply custom formatting to the Excel cells.

  const conf = {};
  conf.stylesXmlFile = 'styles.xml';
  conf.cols = [
    { caption: 'Name', type: 'string', beforeCellWrite: function() { return function(data) { return data.toUpperCase(); }; } },
    { caption: 'Age', type: 'number' }
  ];
  conf.rows = [
    ['Alice', 25],
    ['Bob', 30]
  ];
  
  const result = excelExport.execute(conf);
  require('fs').writeFileSync('styled_output.xlsx', result, 'binary');

3. Multiple Sheets

Export data to multiple sheets within the same Excel file.

  const conf1 = { name: 'Sheet1', cols: [{ caption: 'A', type: 'string' }], rows: [['Hello']] };
  const conf2 = { name: 'Sheet2', cols: [{ caption: 'B', type: 'string' }], rows: [['World']] };
  
  const result = excelExport.execute([conf1, conf2]);
  require('fs').writeFileSync('multi_sheet_output.xlsx', result, 'binary');

4. Merging Cells

Merge cells in the Excel file.

  const XLSX = require('xlsx');
  const workbook = XLSX.utils.book_new();
  const worksheet = XLSX.utils.aoa_to_sheet([['A1', 'B1'], ['A2', 'B2']]);
  
  XLSX.utils.sheet_add_aoa(worksheet, [['Merged Cell', '']], { origin: 'A1' });
  worksheet['!merges'] = [{ s: { r: 0, c: 0 }, e: { r: 0, c: 1 } }];
  
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
  XLSX.writeFile(workbook, 'merged_cells_output.xlsx');

App Example

Let’s create an application that exports user data to an Excel file.

  const express = require('express');
  const excelExport = require('excel-export');
  const app = express();

  app.get('/download', (req, res) => {
    const conf = {};
    conf.cols = [
      { caption: 'Name', type: 'string' },
      { caption: 'Age', type: 'number' }
    ];
    conf.rows = [
      ['Alice', 25],
      ['Bob', 30]
    ];
    
    const result = excelExport.execute(conf);
    res.setHeader('Content-Disposition', 'attachment; filename="users.xlsx"');
    res.end(result, 'binary');
  });
  
  app.listen(3000, () => {
    console.log('Server is running on port 3000');
  });

With this setup, users can download an Excel file containing user data by navigating to /download on your server.

Hash: 2d1590bd4e1d5a17d87278976e5fea7af00660160dae10f9e1df61e8a7b6f9ca

Leave a Reply

Your email address will not be published. Required fields are marked *