使用 GraalJS 进行数据处理
GraalJS 是 Java 的 JS 引擎。该项目提供了兼容 JSR-223 的 OpenJDK 兼容版本。
¥GraalJS is a JS engine for Java. The project offers a JSR-223 compliant OpenJDK-compatible build.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
"完整示例" 部分包括一个完整的 Java 命令行工具,用于从电子表格读取数据和打印 CSV 行。
¥The "Complete Example" section includes a complete Java command-line tool for reading data from spreadsheets and printing CSV rows.
本演示中使用的 Java 包受开源许可证保护。通用许可许可证涵盖了大多数软件包,而 icu4j 使用不同但仍然许可的许可证。
¥The Java packages used in this demo are covered under open source licenses. The
Universal Permissive License covers most of the packages, while icu4j uses
a different, yet still permissive, license.
集成详情
¥Integration Details
上次测试此演示时,原始字节数组无法传递给 GraalJS。
¥When this demo was last tested, raw byte arrays could not be passed to GraalJS.
这是默认 GraalJS 行为的限制。
¥This is a limitation of the default GraalJS behavior.
相反,此演示通过运行时标志使用 Nashorn 兼容模式 [^1]:
¥Instead, this demo uses Nashorn Compatibility Mode[^1] through a runtime flag:
java -Dpolyglot.js.nashorn-compat=true ...
纳肖恩演示 代码和解释适用于 JSR-223 兼容的 ScriptEngine 实现,包括 GraalJS。
¥The Nashorn demo code and explanation applies to
JSR-223 compatible ScriptEngine implementations, including GraalJS.
完整示例
¥Complete Example
该演示在以下部署中进行了测试:
¥This demo was tested in the following deployments:
| OpenJDK | GraalJS | 日期 | 
|---|---|---|
| 24.0.1 | 24.2.1 | 2025-05-06 | 
| 23.0.2 | 24.2.1 | 2025-05-06 | 
| 22.0.2 | 24.2.1 | 2025-05-06 | 
| 21.0.7 | 24.2.1 | 2025-05-06 | 
| 20.0.2 | 24.2.1 | 2025-05-06 | 
| 19.0.2 | 24.2.1 | 2025-05-06 | 
| 18.0.2 | 24.2.1 | 2025-05-06 | 
| 17.0.15 | 24.2.1 | 2025-05-06 | 
汇编
¥Compilation
- 
下载 GraalJS 及其依赖: ¥Download GraalJS and its dependencies: 
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-scriptengine/24.2.1/js-scriptengine-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-language/24.2.1/js-language-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/polyglot/polyglot/24.2.1/polyglot-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/collections/24.2.1/collections-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/truffle/truffle-api/24.2.1/truffle-api-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/nativeimage/24.2.1/nativeimage-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/shadowed/icu4j/24.2.1/icu4j-24.2.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/regex/regex/24.2.1/regex-24.2.1.jar"
- 
下载 SheetJS Standalone 脚本、shim 脚本和测试文件。将所有三个文件移动到项目目录: ¥Download the SheetJS Standalone script, shim script and test file. Move all three files to the project directory: 
curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/shim.min.js
curl -LO https://xlsx.nodejs.cn/pres.xlsx
- 
¥Download SheetJSNashorn.java:
curl -LO https://xlsx.nodejs.cn/nashorn/SheetJSNashorn.java
- 
构建示例类: ¥Build the sample class: 
javac SheetJSNashorn.java
该程序尝试解析第一个参数指定的文件并打印第一个工作表中的 CSV 行。
¥This program tries to parse the file specified by the first argument and prints CSV rows from the first worksheet.
独立测试
¥Standalone Test
- 
直接运行命令: ¥Run the command directly: 
- Linux/MacOS
- Windows
java -cp ".:js-scriptengine-24.2.1.jar:js-language-24.2.1.jar:polyglot-24.2.1.jar:collections-24.2.1.jar:truffle-api-24.2.1.jar:nativeimage-24.2.1.jar:icu4j-24.2.1.jar:regex-24.2.1.jar" -Dpolyglot.js.nashorn-compat=true SheetJSNashorn pres.xlsx
java -cp ".;js-scriptengine-24.2.1.jar;js-language-24.2.1.jar;polyglot-24.2.1.jar;collections-24.2.1.jar;truffle-api-24.2.1.jar;nativeimage-24.2.1.jar;icu4j-24.2.1.jar;regex-24.2.1.jar" -D"polyglot.js.nashorn-compat=true" SheetJSNashorn pres.xlsx
如果成功,将显示第一个工作表中的 CSV 行。
¥If successful, CSV rows from the first worksheet will be displayed.
Java 存档测试
¥Java Archive Test
- 
组装 Java 存档: ¥Assemble a Java Archive: 
jar -cf SheetJSNashorn.jar SheetJSNashorn.class xlsx.full.min.js shim.min.js
- 
创建新目录并复制存档和测试文件: ¥Create new directory and copy the archives and test file: 
mkdir -p sheethorn
cp *.jar pres.xlsx sheethorn
cd sheethorn
- 
使用 Java Archive 运行程序: ¥Run the program using the Java Archive: 
- Linux/MacOS
- Windows
java -cp ".:js-scriptengine-24.2.1.jar:js-language-24.2.1.jar:polyglot-24.2.1.jar:collections-24.2.1.jar:truffle-api-24.2.1.jar:nativeimage-24.2.1.jar:icu4j-24.2.1.jar:regex-24.2.1.jar:SheetJSNashorn.jar" -Dpolyglot.js.nashorn-compat=true  SheetJSNashorn pres.xlsx
java -cp ".;js-scriptengine-24.2.1.jar;js-language-24.2.1.jar;polyglot-24.2.1.jar;collections-24.2.1.jar;truffle-api-24.2.1.jar;nativeimage-24.2.1.jar;icu4j-24.2.1.jar;regex-24.2.1.jar;SheetJSNashorn.jar" -D"polyglot.js.nashorn-compat=true"  SheetJSNashorn pres.xlsx
这应该打印与步骤 4 相同的 CSV 行。
¥This should print the same CSV rows from Step 4.
[^1]: 请参阅 GraalJS 文档中的 "Nashorn 兼容模式"。
¥See "Nashorn Compatibility Mode" in the GraalJS documentation.