Skip to main content

CapacitorJS

The SheetJS NodeJS Module can be imported from any component or script in the app.

The "Complete Example" creates an app that looks like the screenshots below:

iOSAndroid

iOS screenshot

Android screenshot

Tested Deployments

This demo was tested in the following environments:

OSTypeDeviceCapacitorJS + FSDate
Android 34SimPixel 3a5.5.1 / 5.1.42023-12-04
iOS 17.0.1SimiPhone 15 Pro Max5.5.1 / 5.1.42023-12-04
Android 29RealNVIDIA Shield5.5.1 / 5.1.42023-12-04
iOS 15.1RealiPad Pro5.5.1 / 5.1.42023-12-04
Telemetry

Before starting this demo, manually disable telemetry. On Linux and MacOS:

npx @capacitor/cli telemetry off

To verify telemetry was disabled:

npx @capacitor/cli telemetry

Integration Details

This example uses Svelte, but the same principles apply to other frameworks.

Reading data

The standard HTML5 File Input element logic works in CapacitorJS:

<script>
import { read, utils } from 'xlsx';

let html = "";

/* show file picker, read file, load table */
async function importFile(evt) {
const f = evt.target.files[0];
const wb = read(await f.arrayBuffer());
const ws = wb.Sheets[wb.SheetNames[0]]; // get the first worksheet
html = utils.sheet_to_html(ws); // generate HTML and update state
}
</script>

<main>
<input type="file" on:change={importFile}/>
<div bind:this={tbl}>{@html html}</div>
</main>

Writing data

@capacitor/filesystem can write Base64 strings:

<script>
import { Filesystem, Directory } from '@capacitor/filesystem';
import { utils, writeXLSX } from 'xlsx';

let html = "";
let tbl;

/* get state data and export to XLSX */
async function exportFile() {
const elt = tbl.getElementsByTagName("TABLE")[0];
const wb = utils.table_to_book(elt);
/* generate Base64 string for Capacitor */
const data = writeXLSX(wb, { type: "base64" });
await Filesystem.writeFile({
data,
path: "SheetJSCap.xlsx",
directory: Directory.Documents
}); // write file
}

</script>

<main>
<button on:click={exportFile}>Export XLSX</button>
<div bind:this={tbl}>{@html html}</div>
</main>

Demo

Base Project

  1. Follow the official "Environment Setup"1 instructions to set up Android and iOS targets

iOS development is only supported on macOS.

Installation Notes (click to show)

CapacitorJS requires Java 17.

  1. Disable telemetry.
npx @capacitor/cli telemetry off

Verify that telemetry is disabled by running

npx @capacitor/cli telemetry

(it should print Telemetry is off)

  1. Create a new Svelte project:
npm create vite@latest sheetjs-cap -- --template svelte
cd sheetjs-cap
  1. Install dependencies:
npm i --save https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz
npm i --save @capacitor/core @capacitor/cli @capacitor/filesystem
  1. Create CapacitorJS structure:
npx cap init sheetjs-cap com.sheetjs.cap --web-dir=dist
npm run build

If prompted to create an Ionic account, type N and press Enter.

  1. Download src/App.svelte and replace:
curl -o src/App.svelte -L https://docs.sheetjs.com/cap/App.svelte

Android

  1. Create Android app
npm i --save @capacitor/android
npx cap add android
  1. Enable file reading and writing in the Android app.

Add the highlighted lines to android/app/src/main/AndroidManifest.xml after the Permissions comment:

android/app/src/main/AndroidManifest.xml (add to file)
    <!-- Permissions -->

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.INTERNET" />
  1. Run the app in the simulator
npm run build
npx cap sync
npx cap run android
  1. Test the app

Open the app and observe that presidents are listed in the table.

Touch "Export XLSX" and the emulator will ask for permission:

Tap "Allow" and a popup will be displayed with a path.

To see the generated file, switch to the "Files" app in the simulator, tap the icon and tap "Documents". Tap "Documents" folder to find SheetJSCap.xlsx.

iOS

  1. Create iOS app
npm i --save @capacitor/ios
npx cap add ios
  1. Enable file sharing and make the documents folder visible in the iOS app. The following lines must be added to ios/App/App/Info.plist:
ios/App/App/Info.plist (add to file)
<plist version="1.0">
<dict>
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>CFBundleDevelopmentRegion</key>

(The root element of the document is plist and it contains one dict child)

  1. Run the app in the simulator
npm run build
npx cap sync
npx cap run ios

If prompted to select a target device, select "iPhone 15 Pro Max (simulator)".

  1. Test the app

Open the app and observe that presidents are listed in the table.

Touch "Export XLSX" and a popup will be displayed.

To see the generated file, switch to the "Files" app in the simulator and look for SheetJSCap.xlsx in "On My iPhone" > "sheetjs-cap"

Android Device

  1. Connect an Android device using a USB cable.

If the device asks to allow USB debugging, tap "Allow".

  1. Close any Android / iOS emulators.

  2. Build APK and run on device:

npm run build
npx cap sync
npx cap run android

If the Android emulators are closed and an Android device is connected, the last command will build an APK and install on the device.

For real devices running API level 29 or below, the following line must be added to the application open tag in android/app/src/main/AndroidManifest.xml:

android/app/src/main/AndroidManifest.xml (add highlighted attribute)
    <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/AppTheme">

iOS Device

  1. Connect an iOS device using a USB cable

  2. Close any Android / iOS emulators.

  3. Enable developer code signing certificates2

  4. Run on device:

npm run build
npx cap sync
npx cap run ios

When prompted to select a target device, select the real device in the list.

Footnotes

  1. See "Environment Setup" in the CapacitorJS documentation.

  2. The Flutter documentation covers the instructions in more detail. The correct workspace is ios/App/App.xcworkspace