4.4 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	| title | pagination_prev | pagination_next | sidebar_position | sidebar_custom_props | ||
|---|---|---|---|---|---|---|
| Tauri | demos/mobile/index | demos/data/index | 4 | 
 | 
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
The NodeJS Module can be imported from JavaScript code.
The "Complete Example" creates an app that looks like the screenshot:
| macOS | Linux | 
|---|---|
Integration Details
:::note
Tauri currently does not provide the equivalent of NodeJS fs module.  The raw
@tauri-apps/api methods used in the examples are not expected to change.
:::
http, dialog, and fs must be explicitly allowed in tauri.conf.json:
    "allowlist": {
      "http": {
        "all": true,
        "request": true,
        "scope": ["https://**"]
      },
      "dialog": {
        "all": true
      },
      "fs": {
        "all": true
      }
Reading Files
There are two steps to reading files: obtaining a path and reading binary data:
import { read } from 'xlsx';
import { open } from '@tauri-apps/api/dialog';
import { readBinaryFile } from '@tauri-apps/api/fs';
const filters = [
  {name: "Excel Binary Workbook", extensions: ["xlsb"]},
  {name: "Excel Workbook", extensions: ["xlsx"]},
  {name: "Excel 97-2004 Workbook", extensions: ["xls"]},
  // ... other desired formats ...
];
async function openFile() {
  /* show open file dialog */
  const selected = await open({
    title: "Open Spreadsheet",
    multiple: false,
    directory: false,
    filters
  });
  /* read data into a Uint8Array */
  const d = await readBinaryFile(selected);
  /* parse with SheetJS */
  const wb = read(d);
  return wb;
}
Writing Files
There are two steps to writing files: obtaining a path and writing binary data:
import { write } from 'xlsx';
import { save } from '@tauri-apps/api/dialog';
import { writeBinaryFile } from '@tauri-apps/api/fs';
const filters = [
  {name: "Excel Binary Workbook", extensions: ["xlsb"]},
  {name: "Excel Workbook", extensions: ["xlsx"]},
  {name: "Excel 97-2004 Workbook", extensions: ["xls"]},
  // ... other desired formats ...
];
async function saveFile(wb) {
  /* show save file dialog */
  const selected = await save({
    title: "Save to Spreadsheet",
    filters
  });
  if(!selected) return;
  /* Generate workbook */
  const bookType = selected.slice(selected.lastIndexOf(".") + 1);
  const d = write(wb, {type: "buffer", bookType});
  /* save data to file */
  await writeBinaryFile(selected, d);
}
Complete Example
:::note
This demo was tested against Tauri v1.2.3 on 2023 February 12.
:::
- 
Read Tauri "Getting Started" guide and install dependencies. 
- 
Create a new Tauri app: 
npm create tauri-app
When prompted:
- Project Name: SheetJSTauri
- Package Manager: npm
- UI template: vue-ts
- Enter the directory and install dependencies:
cd SheetJSTauri
npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz
npm i --save @tauri-apps/api
npm i --save-dev @tauri-apps/cli
- Enable operations by adding the highlighted lines to tauri.conf.jsonin thetauri.allowlistsection:
  "tauri": {
    "allowlist": {
// highlight-start
      "http": {
        "all": true,
        "request": true,
        "scope": ["https://**"]
      },
      "dialog": {
        "all": true
      },
      "fs": {
        "all": true
      },
// highlight-end
In the same file, look for the "identifier" key and replace the value with com.sheetjs.tauri:
        "icons/icon.ico"
      ],
      // highlight-next-line
      "identifier": "com.sheetjs.tauri",
      "longDescription": "",
- Download App.vueand replacesrc/App.vuewith the downloaded script.
curl -L -o src/App.vue https://docs.sheetjs.com/tauri/App.vue
- Build the app with
npm run tauri build
At the end, it will print the path to the generated program. Run the program!

