linc-technologies / ember-spreadsheet-export

Addon that encapsulates ability to render a data set as either excel or csv.
MIT License
12 stars 8 forks source link
addon

@linc-technologies/ember-spreadsheet-export

Addon that encapsulates ability to render a data set as either excel or csv.

Compatibility

Installation

ember install @linc-technologies/ember-spreadsheet-export

Usage

Merging Cells (Excel only)

In order to merge cells, an array of merges can be passed in the options hash. Each element of this array should be an object taking the following form:

{
  s: {
    r: 0,
    c: 0,
  },
  e: {
    r: 1,
    c: 0,
  },
}

s defines the start of the range to be merged, and e the end of the range. Within each, r is the row index and c is the column index. The example above would therefore merge the first two cells in the first column.

Examples

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';

export default class MyComponent extends Component {
  // Don't forget to inject the service(s) as needed
  @service csv;
  @service excel;

  // Then go ahead and use it in your code
  @action export() {
    const data = [
      ['Title 1', 'Title 2', 'Title 3'],
      ['row1cell1', 'row1cell2', 'row1cell3'],
      ['row2cell1', 'row2cell2', 'row2cell3'],
    ];

    if (type === 'MultiExcel') {
      const sheets = [
        {
          name: 'Demo sheet',
          data,
        },
        {
          name: 'Supplemental sheet',
          data: [
            ['Foo', 'Bar'],
            ['Baz', 'Foobar'],
          ],
        },
        {
          name: 'HTML table',
          data: document.querySelector('#data'),
        },
      ];
      this.excel.export(sheets, { multiSheet: true, fileName: 'test.xlsx' });
    } else if (type === 'Excel') {
      this.excel.export(data, { sheetName: 'sheet1', fileName: 'test.xlsx' });
    } else if (type === 'CSV') {
      this.csv.export(data, { fileName: 'test.csv', autoQuote: true });
    }
  }
}

Differences from ember-cli-data-export

This was initially forked from roofstock/ember-cli-data-export.