React Native Tutorial to Export Raw JSON Data to Excel (XLSX) File in Browser Using Javascript Full Project For Beginners

 

 

You can convert JSON to excel file

You have to use two pacakge

  1. react-native-fs
  2. XLSX (for convert json to excel file)

here is an example

 

 

import { writeFile, readFile } from 'react-native-fs';
import XLSX from 'xlsx';

var data = [
{"name":"John", "city": "Seattle"},
{"name":"Mike", "city": "Los Angeles"},
{"name":"Zach", "city": "New York"}
];

 var ws = XLSX.utils.json_to_sheet(data);

  var wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb,ws,"Prova");

  const wbout = XLSX.write(wb, {type:'binary', bookType:"xlsx"});
  var RNFS = require('react-native-fs');
  var file = RNFS.ExternalStorageDirectoryPath + '/test.xlsx';
  writeFile(file, wbout, 'ascii').then((r)=>{/* :) */}).catch((e)=>{/* :( */});

 

 

if you want to read Excel file

 

 

import { writeFile, readFile } from 'react-native-fs';
import XLSX from 'xlsx';

  const filePath="/Users/copoo/Downloads/Data.xlsx";
  const excelFile=await RNFS.readFile(filePath,'ascii');
  const workbook = XLSX.read(excelFile, {type:'binary'});
  console.log(workbook,"excelFile")

Leave a Reply