找回密码
 立即注册

QQ登录

只需一步,快速开始

alizee10251 讲师达人认证 悬赏达人认证 SpreadJS 开发认证

银牌会员

98

主题

150

帖子

2758

积分

银牌会员

积分
2758

活字格认证活字格高级认证微信认证勋章

alizee10251 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
银牌会员   /  发表于:2019-7-2 14:03  /   查看:3446  /  回复:0
本帖最后由 alizee1025 于 2019-7-2 15:06 编辑

一、SpreadJS 简介
SpreadJS是一款基于 HTML5 的纯 JavaScript 电子表格和网格功能控件,以“高速低耗、纯前端、零依赖”为产品特色,可嵌入任何操作系统,同时满足 .NET、Java、响应式 Web 应用及移动跨平台的表格数据处理和类Excel 的表格应用开发,为终端用户带来亲切的 Excel 体验。本文将以xlsx文件格式为例,展示如何使用SpreadJS实现前端导入和导出excel文件。


1.    主要功能
  • 功能、UI 与 Excel 高度类似
  • 兼容 450 种以上的 Excel 公式
  • 符合 UMD 规范,可按需加载
  • 完善的数据可视化,支持形状、图表、迷你图
  • 纯前端导入、导出 Excel 文件
  • 使用 HTML5 图形(Canvas)绘制界面,具备高性能和响应速度


2.   安装包目录结构


├──Spread.Sheets        SpreadJS产品包
│         └── Designer      SpreadJS表格设计器
│                      ├── Spread.Sheets-Designer.12.0.0.AppImage   [Mac]
│                      ├── Spread.Sheets-Designer.12.0.0.dmg          [Linux]
│                      └── Spread.Sheets-Designer.12.0.0                [Windows]
│        └── Spread.Sheets.Docs.12.0.0   SpreadJS 表格接口文档
│                      ├── content
│                      └── index   
│        └── Spread.Sheets.Release.12.0.0   SpreadJS 表格 JavaScript 库/演示用例
│                      ├── css             样式文件
│                      ├── definition   TS 引用文件
│                      ├── readme
│                      ├── samples      示例代码(包括原生JS,Angular,Vue,React)
│                      ├── scripts         JS文件
│                      ├── GrapeCity-EULA
│                      └── LICENSE


3.   如何使用

1. Spread.Sheets不依赖任何第三方组件。它只需要引用下列文件:gc.spread.sheets.xx.x.x.css,gc.spread.sheets.all.xx.x.x.min.js。


  1. <linkrel="styleSheet" href="gc.spread.sheets.xx.x.x.css" />
  2. <scriptsrc="gc.spread.sheets.all.xx.x.x.min.js"type="text/javascript"></script>
  3. <scriptsrc="gc.spread.sheets.resources.zh.xx.x.x.min.js"type="text/javascript"></script>
复制代码


2. 在页面的body元素中添加一个DOM元素作为它的容器。


  1. <divid="ss" style="width:100%; height:360px;border: 1px solidgray;"></div>
复制代码



3. 用代码“newGC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 })”来初始化Spread。


  1. window.onload= function () {
  2.     var spread = newGC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 });
  3. };
复制代码



image.png516631774.png




二、前端导入导出
Excel


实现前端导入导出只需要引入gc.spread.excelio库,使用excelIO.open 和excelIO.save两个方法即可,不需要配置任何选项,代码十分简洁易懂。

image.png141967772.png

具体步骤如下:

前端导入导出支持将Spread json导出为excel文件(.xlsx)和将excel文件导入为Spread json.
使用前端导入导出, 你需要将相关的js文件添加的document的head区域。例如:
  1. <head>

  2.    ...

  3.    <script src='.../spreadjs/gc.spread.sheets.all.x.xx.xxxxx.x.min.js' type='text/javascript'></script>

  4.    <script src='.../spreadjs/plugins/gc.spread.excelio.x.xx.xxxxx.x.min.js' type='text/javascript'></script>

  5. </head>
复制代码

初始化Workbook实例和ExcelIO实例:
  1. var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'));

  2. var excelIo = new GC.Spread.Excel.IO();
复制代码

接下来你就可以使用open方法将excel文件导入为spread json,使用 save 方法将spread json导出为excel文件。例如:
  1. //import excel file to Spread.Sheets json

  2. excelIo.open(excelFile, function (json) {

  3.     var workbookObj = json;

  4.     workbook.fromJSON(workbookObj);

  5. }, function (e) {

  6.     // process error

  7.     console.log(e);

  8. });

  9. //export Spread.Sheets json to excel file

  10. excelio.save(json, function (blob) {

  11.     //do whatever you want with blob

  12.     //such as you can save it

  13. }, function (e) {

  14.     //process error

  15.     console.log(e);

  16. });
复制代码

同时,你还可打开或保存一个带有密码保护的excel文件,只需要在open和save方法中传入参数options{password:xxxx}即可。例如:
  1. //import excel file to Spread.Sheets json

  2. excelIo.open(excelFile, function (json) {

  3.     var workbookObj = json;

  4.     workbook.fromJSON(workbookObj);

  5. }, function (e) {

  6.     // process error

  7.     console.log(e);

  8. },{password:xxxx});

  9. //export Spread.Sheets json to excel file

  10. excelio.save(json, function (blob) {

  11.     //do whatever you want with blob

  12.     //such as you can save it

  13. }, function (e) {

  14.     //process error

  15.     console.log(e);

  16. },{password:xxxx});
复制代码
前端导入导出Excel 的示例源码及数据源下载:

excel_data.js (31.91 KB, 下载次数: 525)

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部