---
title: Sheets in .NET with Jurassic
sidebar_label: C# + Jurassic
pagination_prev: demos/bigdata/index
pagination_next: solutions/input
---
import current from '/version.js';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';
Jurassic[^1] is a JavaScript compiler for .NET, In contrast to other engines,
Jurassic generates .NET bytecode.
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
This demo uses Jurassic and SheetJS to read and write spreadsheets. We'll
explore how to load SheetJS in the Jurassic engine, exchange binary data with a
C# program, and process spreadsheets and structured data.
The ["Integration Example"](#integration-example) section includes a complete
command-line tool for reading arbitrary workbooks and writing data to ODS
(OpenDocument Spreadsheet) workbooks.
:::danger Telemetry
**The `dotnet` command embeds telemetry.**
The `DOTNET_CLI_TELEMETRY_OPTOUT` environment variable should be set to `1`.
["Platform Configuration"](#platform-configuration) includes instructions for
setting the environment variable on supported platforms.
:::
## Integration Details
The [SheetJS "mini" script](/docs/getting-started/installation/standalone) can
be parsed and evaluated in a Jurassic engine instance.
:::warning pass
Jurassic throws errors when processing the "full" script (`xlsx.full.min.js`):
```
Unhandled exception. Jurassic.JavaScriptException: Error: Maximum number of named properties reached.
```
**The `xlsx.mini.min.js` script must be used!**
The mini build has a number of limitations, as noted in the installation guide.
:::
It is recommended to pass Base64 strings between C# code and the script engine.
### Initialize Jurassic
A `Jurassic.ScriptEngine` object can be created in one line:
```csharp
var engine = new Jurassic.ScriptEngine();
```
Jurassic does not expose the NodeJS `global`. It can be synthesized:
:::note pass
The goal is to run the following JavaScript code:
```js
var global = (function(){ return this; }).call(null);
```
:::
```csharp
engine.Evaluate("var global = (function(){ return this; }).call(null);");
```
### Load SheetJS Scripts
Jurassic engine objects support the `ExecuteFile` method for evaluating scripts:
```csharp
/* read and evaluate the shim script */
engine.ExecuteFile("shim.min.js");
/* read and evaluate the main library */
engine.ExecuteFile("xlsx.mini.min.js");
```
To confirm the library is loaded, `XLSX.version` can be inspected:
```csharp
Console.WriteLine("SheetJS version {0}", engine.Evaluate("XLSX.version"));
```
### Reading Files
In C#, `System.IO.File.ReadAllBytes` reads file data into a `byte[]` byte array:
```csharp
string filename = "pres.xlsx";
byte[] buf = File.ReadAllBytes(filename);
```
The bytes cannot be directly passed to Jurassic. Base64 strings are supported.
An encoded Base64 string can be created with `System.Convert.ToBase64String`:
```csharp
string b64 = System.Convert.ToBase64String(buf);
```
`Jurassic.ScriptEngine#SetGlobalValue` will assign the C# String to a global:
```csharp
engine.SetGlobalValue("buf", b64);
```
The `buf` variable can be parsed from JS with the SheetJS `read` method[^2]:
:::note pass
The following script will be evaluated:
```js
var wb = XLSX.read(buf, {type:'base64'});
```
:::
```csharp
engine.Evaluate("var wb = XLSX.read(buf, {type:'base64'});");
```
`wb` is a SheetJS workbook object. The ["SheetJS Data Model"](/docs/csf) section
describes the object structure and the ["API Reference"](/docs/api) section
describes various helper functions.
### Writing Files
The SheetJS `write` method[^3] can write workbooks. The option `type: "base64"`
instructs the library to generate Base64 strings. The `bookType` option[^4]
controls the output file format.
:::note pass
The following expression will be evaluated:
```js
XLSX.write(wb, {bookType: 'ods', type: 'base64'})
```
The result will be passed back to C# code.
:::
```csharp
string ods = engine.Evaluate("XLSX.write(wb, {bookType: 'ods', type: 'base64'})") as string;
```
`ods` is a Base64 string. `System.Convert.FromBase64String` can decode the
string into a `byte[]` which can be written to file:
```csharp
File.WriteAllBytes("SheetJSJurassic.ods", System.Convert.FromBase64String(ods));
```
## Integration Example
:::note Tested Deployments
This demo was tested in the following deployments:
| Architecture | Jurassic | Date |
|:-------------|:---------|:-----------|
| `darwin-x64` | `3.2.7` | 2024-06-15 |
| `darwin-arm` | `3.2.7` | 2024-06-15 |
| `win10-x64` | `3.2.7` | 2024-06-21 |
| `linux-x64` | `3.2.7` | 2024-06-20 |
| `linux-arm` | `3.2.7` | 2024-06-20 |
:::
### Platform Configuration
0) Set the `DOTNET_CLI_TELEMETRY_OPTOUT` environment variable to `1`.
How to disable telemetry (click to hide)
Installation Notes (click to show)
For macOS x64 and ARM64, install the `dotnet-sdk` Cask with Homebrew:
```bash
brew install --cask dotnet-sdk
```
For Steam Deck Holo and other Arch Linux x64 distributions, the `dotnet-sdk` and
`dotnet-runtime` packages should be installed using `pacman`:
```bash
sudo pacman -Syu dotnet-sdk dotnet-runtime
```
https://dotnet.microsoft.com/en-us/download/dotnet/6.0 is the official source
for Windows and ARM64 Linux versions.
The terminal should display SheetJS version {current}