/* sheetjs (C) 2013-present SheetJS -- https://sheetjs.com */ import { utils } from 'xlsx'; import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, Button, Alert, Image, ScrollView } from 'react-native'; import { Table, Row, Rows, TableWrapper } from 'react-native-table-component'; import { read, write } from 'xlsx'; import { getDocumentAsync } from 'expo-document-picker'; import { documentDirectory, readAsStringAsync, writeAsStringAsync } from 'expo-file-system'; async function pickAndParse() { const result = await getDocumentAsync({copyToCacheDirectory: true}); const path = result.uri; const res = await readAsStringAsync(path, { encoding: "base64" }); return read(res, {type: 'base64'}); } async function writeWorkbook(wb) { const wbout = write(wb, {type:'base64', bookType:"xlsx"}); const file = documentDirectory + "sheetjsw.xlsx"; await writeAsStringAsync(file, wbout, { encoding: "base64" }); return file; }; const make_width = ws => { const aoa = utils.sheet_to_json(ws, {header:1}), res = []; aoa.forEach((r) => { r.forEach((c, C) => { res[C] = Math.max(res[C]||60, String(c).length * 10); }); }); for(let C = 0; C < res.length; ++C) if(!res[C]) res[C] = 60; return res; }; class SheetJSRN extends Component { constructor(props) { super(props); this.state = { data: ["SheetJS".split(""),[5,4,3,3,7,9,5],[8,6,7,5,3,0,9]], widthArr: Array.from({length:7}, () => 20) }; this.importFile = this.importFile.bind(this); this.exportFile = this.exportFile.bind(this); }; async importFile() { try { /* select and parse file */ const wb = await pickAndParse(); /* convert first worksheet to AOA */ const wsname = wb.SheetNames[0]; const ws = wb.Sheets[wsname]; const data = utils.sheet_to_json(ws, {header:1}); /* update state */ this.setState({ data: data, widthArr: make_width(ws) }); } catch(err) { Alert.alert("importFile Error", "Error " + err.message); }} async exportFile() { try { /* convert AOA back to worksheet */ const ws = utils.aoa_to_sheet(this.state.data); /* build new workbook */ const wb = utils.book_new(); utils.book_append_sheet(wb, ws, "SheetJS"); /* write file */ const res = await writeWorkbook(wb); Alert.alert("exportFile success", "Exported to " + res); } catch(err) { Alert.alert("exportFile Error", "Error " + err.message); }} render() { return (   SheetJS × React Native