Skip to main content

使用 Rhino 驯服数据

Rhino 是 Java 中的 ES3+ 引擎。

¥Rhino is an ES3+ engine in Java.

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.

Rhino 不支持 Uint8Array,因此无法读取或写入 NUMBERS 文件。

¥Rhino does not support Uint8Array, so NUMBERS files cannot be read or written.

测试部署

该演示在以下部署中进行了测试:

¥This demo was tested in the following deployments:

OpenJDKRhino日期
23.0.11.7.152025-01-10
22.0.21.7.152025-01-10
21.0.51.7.152025-01-10
20.0.21.7.152025-01-10
19.0.21.7.152025-01-10
18.0.21.7.152025-01-10
17.0.131.7.152025-01-10
16.0.21.7.152025-01-10
15.0.21.7.152025-01-10
14.0.21.7.152025-01-10
13.0.21.7.152025-01-10
12.0.21.7.152025-01-10
11.0.251.7.152025-01-10
10.0.21.7.152025-01-10
9.0.41.7.152025-01-10
1.8.01.7.152025-01-10

集成详情

¥Integration Details

由于代码生成错误,必须关闭优化:

¥Due to code generation errors, optimization must be turned off:

Context context = Context.enter();
context.setOptimizationLevel(-1);

SheetJS 独立脚本 可以在 Rhino 上下文中进行解析和评估。

¥The SheetJS Standalone scripts can be parsed and evaluated in a Rhino context.

二进制字符串可以来回传递。

¥Binary strings can be passed back and forth.

初始化 Rhino

¥Initialize Rhino

Rhino 不提供 global 变量。它可以被创建:

¥Rhino does not provide a global variable. It can be created:

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();

/* Disable optimization */
cx.setOptimizationLevel(-1);

/* Initialize `global` variable */
String s = "var global = (function(){ return this; }).call(null);";
cx.evaluateString(scope, s, "<cmd>", 1, null);

加载 SheetJS 脚本

¥Load SheetJS Scripts

可以通过读取 Java Archive 中的脚本并在 Rhino 上下文中进行评估来加载主库:

¥The main library can be loaded by reading the scripts from the Java Archive and evaluating in the Rhino context:

/* pull source from JAR */
String s = new Scanner(
SheetJS.class.getResourceAsStream("/xlsx.full.min.js")
).useDelimiter("\\Z").next();
/* evaluate */
cx.evaluateString(scope, s, "<cmd>", 1, null);

要确认库已加载,可以检查 XLSX.version

¥To confirm the library is loaded, XLSX.version can be inspected:

/* get handle to XLSX */
Object XLSX = scope.get("XLSX", scope);
if(XLSX == Scriptable.NOT_FOUND) throw new Exception("XLSX not found");

/* get the version string */
String version = ((NativeObject)XLSX).get("version", scope).toString();
System.out.println("SheetJS version " + version);

读取文件

¥Reading Files

可以从 byte 数组生成二进制字符串:

¥A binary string can be generated from a byte array:

  static String read_file(String file) throws IOException {
/* read file -> array of bytes */
byte[] b = Files.readAllBytes(Paths.get(file));

/* construct binary string */
StringBuilder sb = new StringBuilder();
for(int i = 0; i < b.length; ++i) sb.append(Character.toString((char)(b[i] < 0 ? b[i] + 256 : b[i])));
return sb.toString();
}

该字符串可以加载到 JS 引擎中并进行处理:

¥This string can be loaded into the JS engine and processed:

  /* options argument */
String os = "q = {'type':'binary', 'WTF':1};";
NativeObject o = (NativeObject)cx.evaluateString(scope, os, "<cmd>", 2, null);

/* set up function arguments */
String data = read_file(path_to_file);
Object args[] = {data, o};

/* call read -> wb workbook */
NativeObject nXLSX = (NativeObject)XLSX;
Function readfunc = (Function)XLSX.get("read", scope);
NativeObject wb = (NativeObject)readfunc.call(cx, scope, nXLSX, args);

wb 将是对 JS 工作簿对象的引用。

¥wb will be a reference to the JS workbook object.

完整示例

¥Complete Example

  1. 确保已安装 Java。

    ¥Ensure Java is installed.

  2. 为项目创建一个文件夹:

    ¥Create a folder for the project:

mkdir sheetjs-java
cd sheetjs-java
  1. 下载 Rhino JAR 并重命名为 rhino.jar

    ¥Download the Rhino JAR and rename to rhino.jar:

curl -L -o rhino.jar https://repo1.maven.org/maven2/org/mozilla/rhino/1.7.15/rhino-1.7.15.jar
  1. 下载 SheetJS Standalone 脚本和测试文件。将这两个文件保存在项目目录中:

    ¥Download the SheetJS Standalone script and the test file. Save both files in the project directory:

curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
curl -LO https://xlsx.nodejs.cn/pres.xlsx
  1. 下载 SheetJSRhino.zip 并解压

    ¥Download SheetJSRhino.zip and unzip

curl -LO https://xlsx.nodejs.cn/rhino/SheetJSRhino.zip
unzip SheetJSRhino.zip
  1. 将以下代码保存到 SheetJSRhino.java

    ¥Save the following code to SheetJSRhino.java:

SheetJSRhino.java
/* sheetjs (C) 2013-present  SheetJS -- https://sheetjs.com */
/* vim: set ts=2: */
import com.sheetjs.SheetJS;
import com.sheetjs.SheetJSFile;
import com.sheetjs.SheetJSSheet;

public class SheetJSRhino {
public static void main(String args[]) throws Exception {
try {
SheetJS sjs = new SheetJS();

/* open file */
SheetJSFile xl = sjs.read_file(args[0]);

/* get sheetnames */
String[] sheetnames = xl.get_sheet_names();
System.err.println(sheetnames[0]);

/* convert to CSV */
SheetJSSheet sheet = xl.get_sheet(0);
String csv = sheet.get_csv();
System.out.println(csv);
} catch(Exception e) { throw e; } finally { SheetJS.close(); }
}
}
  1. 从演示代码中组装 SheetJS.jar

    ¥Assemble SheetJS.jar from the demo code:

javac -cp ".:rhino.jar" SheetJSRhino.java
jar -cf SheetJS.jar SheetJSRhino.class com/sheetjs/*.class xlsx.full.min.js
  1. 测试程序:

    ¥Test the program:

java -cp ".:SheetJS.jar:rhino.jar" SheetJSRhino pres.xlsx

如果成功,CSV 将打印到控制台。

¥If successful, a CSV will be printed to console.