How to export data to pdf (xlxs, docx or ppt etc.) in both web and android using React Native?

2.9k Views Asked by At

Android show error on export excel library

import ReactToExcel from 'react-html-table-to-excel

This library run perfectly in web but unable to run in android, I'm not sure if the android could not detect the library or this library is not suitable for android. Can someone please me help and thanks!

1

There are 1 best solutions below

2
Ishara Sandun On

What you have used is a React.js package. It cannot be used in React-Native.

To PDF : If you want to convert your data to pdf in react native you can use the following package.

https://www.npmjs.com/package/react-native-html-to-pdf

Table to excel: You don't have to use a package for a data file to be used in Excel. You can create a .json file which can be imported to excel.

But if you really want to create an excel sheet, you can use XLSX library with react-native-fs package.

Sample code is below

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

var data = [
{"name":"Andre", "age": 10},
{"name":"Mikel", "age": 16},
{"name":"John", "city": 19}
];

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

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

  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)=>{/* :( */});

Hope this helps