[]
SpreadJS 支持通过以下方式导入和导出 Excel 文件:
在客户端导入和导出 Excel 文件
使用 SJS 文件格式进行导入和导出
导入和导出加密的 Excel 文件
在客户端,可通过 import 和 export 方法将 Excel 文件(.xlsx、.xlsm、.xltm)导入为 JSON 格式,或将 JSON 对象导出为 Excel 格式。
导出的 Excel 文件会处于锁定状态,原因是该文件来源于互联网。Excel 文件的导出过程发生在客户端而非服务器端,因此客户端下载操作会导致文件被锁定。所以,首次打开该 Excel 文件时,会显示一条警告信息。
需注意,必须包含 <!DOCTYPE html> 才能确保组件正确显示,且需引用文件 gc.spread.sheets.io.x.x.x.min.js 以实现 Excel 文件的导入和导出功能。
你可在本地下载 Excel 文件,也可获取返回的 Excel 文件(类型为 blob)并将其发送至服务器。
以下示例展示了 Excel 文件的导入和导出操作。请注意,代码第一部分列出了客户端导入和导出所需的依赖文件。
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/gc.spread.sheets.excel2013white.x.x.x.css" rel="stylesheet" />
<script src="scripts/gc.spread.sheets.all.x.x.x.min.js" type="application/javascript"></script>
<script src="scripts/gc.spread.sheets.io.x.x.x.min.js"></script>
<script src="scripts/FileSaver.js"></script>
<script>
var spread;
window.onload = function () {
// 初始化 SpreadJS 工作簿
spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
}
// 将 xlsx、ssjson、csv 文件导入到 spread 中
function ImportFile() {
var file = document.getElementById("fileDemo").files[0];
spread.import(file, function () {
// 成功回调函数,可在此处执行后续操作
}, function (e) {
console.log(e); // 错误回调函数,打印错误信息
}, {
fileType: GC.Spread.Sheets.FileType.excel // 指定文件类型为 Excel
});
}
// 将 spread 导出为 xlsx、ssjson、csv 文件
function ExportFile() {
var fileName = "fileNamehere.xlsx";
spread.export(function (blob) {
// 将 blob 对象保存为文件
saveAs(blob, fileName);
}, function (e) {
console.log(e); // 错误回调函数,打印错误信息
}, {
fileType: GC.Spread.Sheets.FileType.excel // 指定文件类型为 Excel
});
}
</script>
</head>
<body>
<div>
<input type="file" name="files[]" id="fileDemo" accept=".xlsx" />
<input type="button" id="loadExcel" value="Import" onclick="ImportFile()" />
<input type="button" class="btn btn-default" id="saveExcel" value="Export" onclick="ExportFile()" />
<div id="ss" style="width:100%; height:500px"></div>
</div>
</body>
</html>可通过 import 和 export 方法将 Excel 文件(.xlsx、.xlsm、.xltm)导入为 Spread 对象,或将 .SJS 文件导入为 Spread 对象,并导出为 Excel 格式。
采用 .SJS 格式时,处理超大型 Excel 文件可显著提升加载速度、优化内存占用,且重新保存时文件体积也会大幅减小。
您可以通过 import 方法导入 Excel 文件,并使用 save 方法保存为 sjs 文件,进行存储管理。
也可以通过 open 方法打开 sjs 文件,并使用 export 方法将 sjs 文件,导出为 Excel 文件。
以下示例展示了如何使用 open 和 export 方法打开 sjs 文件并导出为 Excel 文件:
// 打开 SJS 文件并加载到 spread 中
function OpenFile() {
var excelFile = document.getElementById('fileDemo').files[0];
spread.open(
excelFile,
() => {}, // 成功回调函数(空实现)
function (e) {
console.log(e); // 错误回调函数,打印错误信息
}
);
}
// 将 spread 导出为 xlsx 文件
function SaveFile() {
var fileName = document.getElementById('exportFileName').value;
// 若文件名后缀不是 .xlsx,则自动添加后缀
if (fileName.substr(-5, 5) !== '.xlsx') {
fileName += '.xlsx';
}
spread.export(
function (blob) {
saveAs(blob, fileName);
},
function (e) {
console.log(e);
},
{
fileType: GC.Spread.Sheets.FileType.excel,
includeBindingSource: true,
}
);
}下表列出了 Excel 文件导入(Excel 到 JSON)和导出(JSON 到 Excel)支持的功能:
类别 | 描述 | 导入(Excel 到 JSON) | 导出(JSON 到 Excel) |
|---|---|---|---|
workbook(spread) | 标签栏:tabStripVisible(标签栏可见性)、startSheetIndex(起始工作表索引)、tabStripRatio(标签栏比例)、tabColor(标签颜色) | √ | √ |
滚动条:showHorizontalScrollbar(水平滚动条可见性)、showVerticalScrollbar(垂直滚动条可见性) | √ | √ | |
工作表:工作表可见性、工作表名称 | √ | √ | |
引用样式:R1C1 或 A1 | √ | √ | |
自定义名称 | √ | √ | |
sharedStrings(共享字符串) | 用于工作表数据的字符串(普通字符串、带空格的字符串) | √ | √ |
主题 | 配色方案 | √ | √ |
字体方案(SpreadJS 无详细字体设置) | √ | √ | |
格式方案(SpreadJS 无格式方案) | × | √ | |
样式 | 单元格样式:所有 SpreadJS 支持的样式 | √ | √ |
不同格式(表格使用的格式、条件格式和筛选器) | √ | √ | |
表格样式 | √ | √ | |
工作表 | 行范围分组、列范围分组 | √ | √ |
行数和列数 | √ | √ | |
showZeros(显示零值) | √ | √ | |
网格线可见性、网格线颜色 | √ | √ | |
行标题和列标题可见性 | √ | √ | |
缩放比例 | √ | √ | |
选区 | √ | √ | |
活动行、活动列 | √ | √ | |
冻结(frozenRowCount 冻结行数、frozenColumnCount 冻结列数) | √ | √ | |
默认行高、默认列宽 | √ | √ | |
列信息:列宽、列可见性、列样式 | √ | √ | |
合并单元格(合并区域) | √ | √ | |
受保护的工作表 | √ | √ | |
行信息:行高、行可见性、行样式 | √ | √ | |
单元格信息:单元格值、单元格公式、单元格样式 | √ | √ | |
自定义名称 | √ | √ | |
条件格式 | √ | √ | |
批注 | √ | √ | |
图片 | √ | √ | |
切片器 | √ | √ | |
迷你图 | √ | √ | |
表格 | √ | √ | |
筛选器 | √ | √ | |
数据验证 | √ | √ | |
分级显示 | √ | √ | |
打印设置 | √ | √ |
SpreadJS 支持对 Excel 启用宏的工作簿文件(.xlsm)和 Excel 启用宏的模板文件(.xltm)进行无损导入和导出。
这意味着你可以在 SpreadJS 环境中上传和保存这类文件,确保嵌入的 VBA 宏在 Microsoft Excel 中重新打开时得以保留且可正常运行。
宏风险
恶意软件警告:Excel 会阻止来自不可信来源(如互联网下载)的宏。用户重新打开导出的文件时,必须手动启用宏。
安全设置:
请遵循 Microsoft 宏安全指南:默认阻止 Internet 宏 | Microsoft Learn。
格式限制
宏丢失:
导出为 .xlsx 格式或 SpreadJS 原生格式(.sjs、.ssjson)时,宏会被移除。
宏不可编辑:
在 SpreadJS 中无法查看、编辑或执行宏。
在 SpreadJS 中修改工作表布局,可能会影响引用特定单元格地址的宏逻辑。
以下示例通过在导入/导出方法中将 excelFileType 参数配置为 'XLSM',演示了 .xlsm 文件的导入和导出操作:
// 导出 .xlsm 文件
var fileName = "fileNamehere.xlsm";
spread.export(function (blob) {
// 将 blob 对象保存为文件
saveAs(blob, fileName);
}, function (e) {
console.log(e); // 错误回调函数,打印错误信息
}, {
fileType: GC.Spread.Sheets.FileType.excel, // 指定文件类型为 Excel
excelFileType: 'XLSM' // 指定 Excel 子类型为 XLSM
});
// 导入 .xlsm 文件
// 获取文件 blob 对象
var file = document.getElementById("fileInput").files[0];
spread.import(file, function () {
// 成功回调函数,可在此处执行后续操作
}, function (e) {
console.log(e); // 错误回调函数,打印错误信息
}, {
fileType: GC.Spread.Sheets.FileType.excel, // 指定文件类型为 Excel
excelFileType: 'XLSM' // 指定 Excel 子类型为 XLSM
});在应用程序中添加以下 IO 引用,即可实现加密 Excel 文件的导入和导出:
<script src="scripts/gc.spread.sheets.io.x.x.x.min.js"></script>
要导入受密码保护的 Excel 文件,需在 spread 的 import 方法中传递 password 属性。若密码正确,文件将成功导入;若密码无效或错误,将触发 errorCallBack 函数并返回错误。通过 ErrorCode 枚举的成员,可自定义错误提示信息。
以下代码示例展示了如何导入加密的 Excel 文件:
const userPassword = 'Asdf0123'; // 用户输入的密码
// 错误回调函数:处理密码相关错误
const errorCallback = (e) => {
if ((e.errorCode = GC.Spread.Sheets.IO.errorCode.invalidPassword)) {
console.log('密码无效!');
} else if ((e.errorCode = GC.Spread.Sheets.IO.errorCode.noPassword)) {
console.log('请输入密码!');
}
};
// 导入加密的 Excel 文件
spread.import(file, () => {}, errorCallback, {
fileType: GC.Spread.Sheets.FileType.excel, // 指定文件类型为 Excel
password: userPassword, // 传递用户输入的密码
});通过在 spread 的 export 方法中指定 password 属性,可导出受密码保护的 Excel 文件。在此场景下,若输入了密码,SpreadJS 将导出加密的 Excel 文件;若未输入密码,则导出未加密的原始 Excel 文件。
以下代码示例展示了如何导出加密的 Excel 文件:
const userPassword = 'ABC0123'; // 用户设置的加密密码
const fileName = 'encrypt-export.xlsx'; // 导出文件名称
// 导出加密的 Excel 文件
spread.export(
(blob) => saveAs(blob, fileName), // 成功回调:保存文件
(e) => console.log(e), // 错误回调:打印错误信息
{
fileType: GC.Spread.Sheets.FileType.excel, // 指定文件类型为 Excel
password: userPassword, // 传递加密密码
}
);在 SpreadJS 设计器中导入或导出受保护的 Excel 文件时,也可提供密码:
导入加密 Excel 文件:点击 文件,选择 导入;在 导入 菜单页面,选择 Excel 文件,设置导入文件参数,在 密码 框中输入密码,最后点击 导入 Excel 文件。

导出加密 Excel 文件:点击 文件,选择 导出;在 导出 菜单页面,选择 Excel 文件,设置导出参数,在 密码 框中输入密码,最后点击 导出 Excel 文件。
