0

I'm using the react-csv plugin to convert the json data to a csv,but this plugin does not work on IE.

Can anyone post any tried and tested way either in react plugin or javascript way wherein I can export my data as excel in all the browsers - IE,Firefox,Safari and Chrome.

bharz629
  • 161
  • 1
  • 3
  • 10
  • Check this question https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side – Ivan Mjartan Oct 16 '17 at 09:57

2 Answers2

1
import Workbook from 'react-excel-workbook'

const data1 = [
  {
    foo: '123',
    bar: '456',
    baz: '789'
  },
  {
    foo: 'abc',
    bar: 'dfg',
    baz: 'hij'
  },
  {
    foo: 'aaa',
    bar: 'bbb',
    baz: 'ccc'
  }
]

const data2 = [
  {
    aaa: 1,
    bbb: 2,
    ccc: 3
  },
  {
    aaa: 4,
    bbb: 5,
    ccc: 6
  }
]

const example = (
  <div className="row text-center" style={{marginTop: '100px'}}>
    <Workbook filename="example.xlsx" element={<button className="btn btn-lg btn-primary">Try me!</button>}>
      <Workbook.Sheet data={data1} name="Sheet A">
        <Workbook.Column label="Foo" value="foo"/>
        <Workbook.Column label="Bar" value="bar"/>
      </Workbook.Sheet>
      <Workbook.Sheet data={data2} name="Another sheet">
        <Workbook.Column label="Double aaa" value={row => row.aaa * 2}/>
        <Workbook.Column label="Cubed ccc " value={row => Math.pow(row.ccc, 3)}/>
      </Workbook.Sheet>
    </Workbook>
  </div>
)

render(example, document.getElementById('app'))
Syscall
  • 16,959
  • 9
  • 22
  • 41
fullstackdev
  • 25
  • 1
  • 3
0

You can export data even in Internet explorer by using this library.

react-data-export

usage of this package is simple,

const dataSet1 = [
    {
        name: "Johson",
        amount: 30000,
        sex: 'M',
        is_married: true
    },
    {
        name: "Monika",
        amount: 355000,
        sex: 'F',
        is_married: false
    },
    {
        name: "John",
        amount: 250000,
        sex: 'M',
        is_married: false
    },
    {
        name: "Josef",
        amount: 450500,
        sex: 'M',
        is_married: true
    }
];

and to export this dataSet,

import React from "react"
import * from "react-data-export"
class App extends React.Component {
const dataSet1 = [
    {
        name: "Johson",
        amount: 30000,
        sex: 'M',
        is_married: true
    },
    {
        name: "Monika",
        amount: 355000,
        sex: 'F',
        is_married: false
    },
    {
        name: "John",
        amount: 250000,
        sex: 'M',
        is_married: false
    },
    {
        name: "Josef",
        amount: 450500,
        sex: 'M',
        is_married: true
    }
];

render() {
    return (
        <ExcelFile>
            <ExcelSheet data={dataSet1} name="Employees">
                <ExcelColumn label="Name" value="name" />
                <ExcelColumn label="Wallet Money" value="amount" />
                <ExcelColumn label="Gender" value="sex" />
                <ExcelColumn label="Marital Status" 
                             value={(col) => col.is_married ? "Married" : "Single"} />
            </ExcelSheet>
        </ExcelFile>
    );
  }
}

let me know if it works for you.

Afzal Ahmad
  • 556
  • 5
  • 20