This commit is contained in:
SheetJS 2022-08-06 18:59:58 -04:00
parent a04ceb6bfb
commit 9612e25631
2 changed files with 50 additions and 1 deletions

View File

@ -135,6 +135,41 @@ import { readFile } from "react-native-fs";
const bstr = await readFile(path, "ascii");
/* bstr is a binary string */
const workbook = XLSX.read(bstr, {type: "binary"});
```
- [`expo-file-system`](https://www.npmjs.com/package/expo-file-system)
:::caution
Some Expo APIs return URIs that cannot be read with `expo-file-system`. This
will manifest as an error:
> Unsupported scheme for location '...'
When using `DocumentPicker.getDocumentAsync`, enable `copyToCacheDirectory`:
```js
import * as DocumentPicker from 'expo-document-picker';
const result = await DocumentPicker.getDocumentAsync({
// highlight-next-line
copyToCacheDirectory: true,
type: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
});
const uri = result.uri;
```
:::
Calling `FileSystem.readAsStringAsync` with `FileSystem.EncodingType.Base64`
encoding returns a promise resolving to a string compatible with `base64` type:
```js
import * as XLSX from "xlsx";
import * as FileSystem from 'expo-file-system';
const b64 = await FileSystem.readAsStringAsync(uri, { encoding: FileSystem.EncodingType.Base64 });
const workbook = XLSX.read(b64, { type: "base64" });
```
</TabItem>

View File

@ -258,7 +258,7 @@ The following libraries have been tested:
- [`react-native-file-access`](https://npm.im/react-native-file-access)
The `base64` encoding returns strings compatible with the `base64` type:
The `base64` encoding accepts Base64 strings compatible with the `binary` type:
```js
import * as XLSX from "xlsx";
@ -282,6 +282,20 @@ const DDP = DocumentDirectoryPath + "/";
const bstr = XLSX.write(workbook, {type:'binary', bookType:"xlsx"});
/* bstr is a binary string */
await writeFile(DDP + "sheetjs.xlsx", bstr, "ascii");
```
- [`expo-file-system`](https://www.npmjs.com/package/expo-file-system)
The `FileSystem.EncodingType.Base64` encoding accepts Base64 strings:
```js
import * as XLSX from "xlsx";
import * as FileSystem from 'expo-file-system';
const DDP = FileSystem.documentDirectory;
const b64 = XLSX.write(workbook, {type:'base64', bookType:"xlsx"});
/* b64 is a base64 string */
await FileSystem.writeAsStringAsync(DDP + "sheetjs.xlsx", b64, { encoding: FileSystem.EncodingType.Base64 });
```
</TabItem>