You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blazor WASM应用中打开SpreadJS v16新增的.sjs格式文件?

在Blazor WASM中加载SpreadJS v16的.sjs压缩文件

问题分析

你的实现思路没问题,但需要调整几个细节来确保.sjs文件能被正确加载:

修正后的代码实现

openFileWithSjs: function (host, sjsFile) {
    try {
        // 确保传入的是Uint8Array类型(Blazor传入的字节数组会映射为此类型)
        const fileData = sjsFile instanceof Uint8Array ? sjsFile : new Uint8Array(sjsFile);
        // 创建Blob时指定MIME类型,帮助SpreadJS识别.sjs压缩格式
        const blob = new Blob([fileData], { type: 'application/octet-stream' });
        const spread = GC.Spread.Sheets.findControl(host);

        if (!spread) {
            console.error("未找到SpreadJS控件实例");
            return "未找到SpreadJS控件实例";
        }

        spread.open(blob, 
            () => {
                console.log(".sjs文件加载成功");
                return ".sjs文件加载成功";
            }, 
            (e) => {
                console.error(".sjs文件加载失败:", e.errorMessage);
                return `加载失败:${e.errorMessage}`;
            }
        );
    } catch (e) {
        console.error("加载.sjs文件时发生异常:", e);
        return `加载异常:${e.message}`;
    }
}

关键注意事项

  • 数据类型校验:Blazor WASM传入JavaScript的字节数组默认是Uint8Array,需确保sjsFile是正确的二进制数据类型,避免因格式错误导致Blob无法被解析。
  • MIME类型指定:创建Blob时明确设置type: 'application/octet-stream',让SpreadJS能准确识别这是.sjs压缩格式的二进制文件。
  • 控件实例检查:增加对spread实例的存在性校验,避免因DOM元素未加载或控件未初始化导致调用失败。
  • 错误信息优化:在回调和捕获块中返回更明确的错误提示,方便Blazor端处理异常情况。

Blazor端调用示例(C#)

// 通过IJSInvoker调用JavaScript方法
var sjsFileBytes = await File.ReadAllBytesAsync("your-file-path.sjs");
var result = await JS.InvokeAsync<string>("openFileWithSjs", spreadHostElement, sjsFileBytes);
Console.WriteLine(result);

内容的提问来源于stack exchange,提问作者Tyler Franklin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:35:16