1.0.6 • Published 4 years ago

@opuscapita/excel-service v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

excel-service

Description

Excel service contains a react component for button to open import file window and JS methods to export data to Excel and import data from Excel

Installation

It seems, that npm handles xlsx and xlsx-styles as different versions of the same component. But xlsx-style requires xlsx to be installed in the same node module tree folder with it. In order to make that happen, please follow the instructions below.

Add

externals: [
  { './cptable': 'var cptable' },
]

to webpack.config.js and install

npm install --save file-saver xlsx xlsx-styles @opuscapita/excel-service

It seems, that npm handles xlsx and xlsx-styles as different versions of the same component. But xlsx-style requires xlsx to be installed in the same node module tree folder with it. In order to make that happen, please follow the instructions below.

If xlsx-styles is installed into node_modules/@opuscapita/excel-service/node_modules do the following: 1. Delete package-lock.json 2. Delete node_modules 3. Run npm install 4. Verify that xlsx-styles doesn't exist in node_modules/@opuscapita/excel-service/node_modules and both xlsx and xlsx-styles exist in the root level of the tree. If xlsx is installed into node_modules/@opuscapita/excel-service/node_modules do the above steps for it, but replace xlsx-styles with xlsx.

Demo

View the DEMO

Builds

UMD

The default build with compiled styles in the .js file. Also minified version available in the lib/umd directory.

CommonJS/ES Module

You need to configure your module loader to use cjs or es fields of the package.json to use these module types. Also you need to configure sass loader, since all the styles are in sass format.

API FileInputLabel

Prop nameTypeDefaultDescription
acceptedFilesstring''String with file formats
labelelement, string'Select file'Label for the button
onChangefunction() => {}Callback on file import

API Excel

MethodInputDescription
exportToExceldata :: List, columns :: array of objects, fileName :: string (optional), digits :: number, array, visibleColumns :: ListExports data with specified columns to an Excel file.
importFromExcelfiles :: array, callback :: functionImports data from an Excel file. Use alert callabck for a failed import operation.
onLoadCallbacke :: event object, columns :: array of objects, visibleColumns :: List (optional)Callback on data import
exportSheetsToExcelsheets :: array of Sheet, fileName :: stringExports data with specified columns to an Excel file.

Sheet

Prop nameTypeDefaultDescription
columnsarray of Columns or arrays[]Column headers. In case of one column hedaer row, array contains Column objects, and in case of several column header rows, array contains arrays of Column objects.
dataarray of arrays or objects[]Either array of arrays, which contains objects, with value prop (to be used when no column headers). Or array of (row) objects, which contains props, which match to column valueKeyPath props values.
dataStyleobjectStyle for data, for more information refer to xlsx-styles cell style syntax
formatterfunctionFormatting function for data values
namestring'Sheet x', where x is sheet's order numberSheet's name
noBordersbooleanfalseTrue, if cell borders
rowsarray of Rows[]Row header.

Column

Prop nameTypeDefaultDescription
headerstringColumn header label
mergenumberCount of cells to be merged
valueKeyPatharraypath to column value
valueOptionsobjectIf valueOptions.multiplier is defined for numeric column, then each values in that column are multiplied by the multiplier

Row

Prop nameTypeDefaultDescription
headerstringRow header label

Code example

import React from 'react';
import { fromJS } from 'immutable';
import { Button, ControlLabel, Grid, Row, Col } from 'react-bootstrap';

import { Excel, FileInputLabel } from '@opuscapita/excel-service';

export default class ExampleView extends React.PureComponent {
  constructor(props) {
    super(props);
    this.columns = this.initializeColumns();
    this.state = { data: this.initializeData() };
  }

  initializeColumns = () => ([
    {
      header: 'String',
      valueKeyPath: ['string'],
      width: 200,
    },
    {
      header: 'Number',
      valueKeyPath: ['number'],
      width: 200,
    },
    {
      header: 'Float',
      valueKeyPath: ['float'],
      width: 200,
    },
  ])

  initializeData = () => {
    const data = [];
    for (let i = 0; i < 10; i += 1) {
      data.push({ string: `Item ${i}`, number: i, float: `${i}.00` });
    }
    return data;
  }

  readExcelData = (e) => {
    const data = Excel.onLoadCallback(e, this.columns);
    this.setState({ data });
  }

  handleExportToExcelClick = () => {
    Excel.exportToExcel(fromJS(this.state.data), this.columns, 'ExampleExport');
  }

  handleImportFromExcelClick = (e) => {
    Excel.importFromExcel(e.target.files, this.readExcelData);
  }

  render() {
    return (
      <Grid fluid>
        <Row>
          {this.columns.map(column => (
            <Col xs={4} key={column.header}>
              <ControlLabel>
                {column.header}
              </ControlLabel>
            </Col>
          ))}
        </Row>
        {this.state.data.map(row => (
          <Row key={row.number}>
            <Col xs={4}>
              {row.string}
            </Col>
            <Col xs={4}>
              {row.number}
            </Col>
            <Col xs={4}>
              {row.float}
            </Col>
          </Row>
        ))}
        <Row>
          <Col xs={12}>
            <Button onClick={this.handleExportToExcelClick}>
              Export to Excel
            </Button>
          </Col>
        </Row>
        <Row>
          <Col xs={12}>
            <Button>
              <FileInputLabel
                acceptedFiles=".xlsx"
                label="Import from Excel"
                onChange={this.handleImportFromExcelClick}
              />
            </Button>
          </Col>
        </Row>
      </Grid>
    );
  }
}
1.0.6

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago