导入和导出

SpreadJS 支持打开和保存为多种流行的文件格式,其中包括 Excel 的 .xlsx/.xlsm/.xltm 格式、.csv 格式、.ssjson 格式(旧版的 SpreadJS 格式)以及全新且速度更快的 .sjs 格式的 SpreadJS 文件。与之前的 SpreadJS 版本相比,新的 .sjs 格式在处理非常大的 Excel 文件时,显著缩短了加载时间并优化了内存使用情况,同时在重新保存文件时大幅减小了文件大小。

为了使用 SpreadJS 文件格式 功能,您需要在文档的头部部分添加相关的 js 文件链接,位于 Spread 链接的下方。例如: SpreadJS 支持打开和保存 sjs 文件格式。它还支持导入和导出 xlsx、ssjson 和 csv 文件格式。 例如: 类 GC.Spread.Sheets.Workbook GC.Spread.Sheets.SaveOptions GC.Spread.Sheets.OpenOptions GC.Spread.Sheets.ImportOptions GC.Spread.Sheets.ExportOptions GC.Spread.Sheets.ExcelFileType GC.Spread.Sheets.ImportXlsxOptions GC.Spread.Sheets.ExportXlsxOptions GC.Spread.Sheets.ImportCsvOptions GC.Spread.Sheets.ExportCsvOptions GC.Spread.Sheets.ImportSSJsonOptions GC.Spread.Sheets.ExportSSJsonOptions
<template> <div class="sample-tutorial"> <div class="sample-container"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet> </gc-worksheet> </gc-spread-sheets> <div id="statusBar"></div> </div> <div class="options-container"> <div class="option-row"> <div class="inputContainer"> <input id="selectedFile" type="file" accept=".sjs, .xlsx, .xlsm, .xltm, .ssjson, .json, .csv" v-on:change="selectedFileChange($event)" /> <button class="settingButton" id="open" v-on:click="open">打开</button> <div class="open-options"> <div class="item" v-show="needShow('open', 'openMode')"> <label for="open-openMode">打开模式</label> <select id="open-openMode" v-model.number="openOptions.openMode"> <option value="0">normal</option> <option value="1">lazy</option> <option value="2">incremental</option> </select> </div> <div class="item" v-show="needShow('open', 'includeStyles')"> <input type="checkbox" id="open-includeStyles" v-model="openOptions.includeStyles" /> <label for="open-includeStyles">包含样式</label> </div> <div class="item" v-show="needShow('open', 'includeFormulas')"> <input type="checkbox" id="open-includeFormulas" v-model="openOptions.includeFormulas" /> <label for="open-includeFormulas">包含公式</label> </div> <div class="item" v-show="needShow('open', 'frozenColumnsAsRowHeaders')"> <input type="checkbox" id="open-frozenColumnsAsRowHeaders" v-model="openOptions.frozenColumnsAsRowHeaders"/> <label for="open-frozenColumnsAsRowHeaders">将冻结列作为行标题</label> </div> <div class="item" v-show="needShow('open', 'frozenRowsAsColumnHeaders')"> <input type="checkbox" id="open-frozenRowsAsColumnHeaders" v-model="openOptions.frozenRowsAsColumnHeaders"/> <label for="open-frozenRowsAsColumnHeaders">将冻结行作为列标题</label> </div> <div class="item" v-show="needShow('open', 'fullRecalc')"> <input type="checkbox" id="open-fullRecalc" v-model="openOptions.fullRecalc" /> <label for="open-fullRecalc">完全重新计算</label> </div> <div class="item" v-show="needShow('open', 'dynamicReferences')"> <input type="checkbox" id="open-dynamicReferences" v-model="openOptions.dynamicReferences" /> <label for="open-dynamicReferences">动态引用</label> </div> <div class="item" v-show="needShow('open', 'calcOnDemand')"> <input type="checkbox" id="open-calcOnDemand" v-model="openOptions.calcOnDemand" /> <label for="open-calcOnDemand">按需计算</label> </div> <div class="item" v-show="needShow('open', 'includeUnusedStyles')"> <input type="checkbox" id="open-includeUnusedStyles" v-model="openOptions.includeUnusedStyles"/> <label for="open-includeUnusedStyles">包含未使用的样式</label> </div> <div class="item" v-show="needShow('open', 'convertSheetTableToDataTable')" > <input type="checkbox" id="open-convertSheetTableToDataTable" v-model="openOptions.convertSheetTableToDataTable"/> <label for="open-convertSheetTableToDataTable">将工作表表格转换为数据表</label> </div> <div class="item" v-show="needShow('open', 'incrementalLoading')"> <input type="checkbox" id="open-incrementalLoading" v-model="openOptions.incrementalLoading" /> <label for="open-incrementalLoading">增量加载</label> </div> <div class="item" v-show="needShow('open', 'encoding')"> <label for="open-encoding">编码</label> <input type="text" id="open-encoding" v-model="openOptions.encoding" /> </div> <div class="item" v-show="needShow('open', 'rowDelimiter')"> <label for="open-rowDelimiter">行分隔符</label> <input type="text" id="open-rowDelimiter" v-model="openOptions.rowDelimiter" /> </div> <div class="item" v-show="needShow('open', 'columnDelimiter')"> <label for="open-columnDelimiter">列分隔符</label> <input type="text" id="open-columnDelimiter" v-model="openOptions.columnDelimiter" /> </div> </div> </div> <div class="inputContainer"> <label for="saveFileType">文件类型:</label> <select id="saveFileType" v-model="saveFileType"> <option value="sjs">SJS</option> <option value="xlsx">Excel</option> <option value="ssjson">SSJson</option> <option value="csv">Csv</option> </select> <button class="settingButton" id="save" v-on:click="save">保存</button> <div class="excelFileTypeContainer" v-show="showExcelFileType()"> <label for="fileType">Excel 文件类型:</label> <div> <select id="saveExcelFileType" v-model="excelFileType"> <option value="XLSX">Workbook (*.xlsx)</option> <option value="XLSM">Macro-Enabled Workbook (*.xlsm)</option> <option value="XLTM">Macro-Enabled Template (*.xltm)</option> </select> </div> </div> <div class="save-options"> <div class="item" v-show="needShow('save', 'includeBindingSource')"> <input type="checkbox" id="save-includeBindingSource" v-model="saveOptions.includeBindingSource"/> <label for="save-includeBindingSource">包含绑定源</label> </div> <div class="item" v-show="needShow('save', 'includeStyles')"> <input type="checkbox" id="save-includeStyles" v-model="saveOptions.includeStyles" /> <label for="save-includeStyles">包含样式</label> </div> <div class="item" v-show="needShow('save', 'includeFormulas')"> <input type="checkbox" id="save-includeFormulas" v-model="saveOptions.includeFormulas" /> <label for="save-includeFormulas">包含公式</label> </div> <div class="item" v-show="needShow('save', 'saveAsView')"> <input type="checkbox" id="save-saveAsView" v-model="saveOptions.saveAsView" /> <label for="save-saveAsView">另存为视图</label> </div> <div class="item" v-show="needShow('save', 'rowHeadersAsFrozenColumns')"> <input type="checkbox" id="save-rowHeadersAsFrozenColumns" v-model="saveOptions.rowHeadersAsFrozenColumns"/> <label for="save-rowHeadersAsFrozenColumns">将行标题作为冻结列</label> </div> <div class="item" v-show="needShow('save', 'columnHeadersAsFrozenRows')"> <input type="checkbox" id="save-columnHeadersAsFrozenRows" v-model="saveOptions.columnHeadersAsFrozenRows"/> <label for="save-columnHeadersAsFrozenRows">将列标题作为冻结行</label> </div> <div class="item" v-show="needShow('save', 'includeAutoMergedCells')"> <input type="checkbox" id="save-includeAutoMergedCells" v-model="saveOptions.includeAutoMergedCells"/> <label for="save-includeAutoMergedCells">包含自动合并的单元格</label> </div> <div class="item" v-show="needShow('save', 'includeCalcModelCache')"> <input type="checkbox" id="save-includeCalcModelCache" v-model="saveOptions.includeCalcModelCache"/> <label for="save-includeCalcModelCache">包含计算模型缓存</label> </div> <div class="item" v-show="needShow('save', 'saveR1C1Formula')" > <input type="checkbox" id="save-saveR1C1Formula" v-model="saveOptions.saveR1C1Formula"/> <label for="save-saveR1C1Formula">保存 R1C1 公式</label> </div> <div class="item" v-show="needShow('save', 'includeUnusedNames')"> <input type="checkbox" id="save-includeUnusedNames" v-model="saveOptions.includeUnusedNames" /> <label for="save-includeUnusedNames">包含未使用的名称</label> </div> <div class="item" v-show="needShow('save', 'includeEmptyRegionCells')"> <input type="checkbox" id="save-includeEmptyRegionCells" v-model="saveOptions.includeEmptyRegionCells"/> <label for="save-includeEmptyRegionCells">包含空区域单元格</label> </div> <div class="item" v-show="needShow('save', 'losslessEditing')" > <input type="checkbox" id="save-losslessEditing" v-model="saveOptions.losslessEditing"/> <label for="save-losslessEditing">无损编辑</label> </div> <div class="item" v-show="needShow('save', 'encoding')"> <label for="save-encoding">编码</label> <input type="text" id="save-encoding" v-model="saveOptions.encoding" /> </div> <div class="item" v-show="needShow('save', 'rowDelimiter')"> <label for="save-rowDelimiter">行分隔符</label> <input type="text" id="save-rowDelimiter" v-model="saveOptions.rowDelimiter" /> </div> <div class="item" v-show="needShow('save', 'columnDelimiter')"> <label for="save-columnDelimiter">列分隔符</label> <input type="text" id="save-columnDelimiter" v-model="saveOptions.columnDelimiter" /> </div> <div class="item" v-show="needShow('save', 'sheetIndex')"> <label for="save-sheetIndex">工作表索引</label> <input type="number" id="save-sheetIndex" v-model.number="saveOptions.sheetIndex" /> </div> <div class="item" v-show="needShow('save', 'row')"> <label for="save-row">行</label> <input type="number" id="save-row" v-model.number="saveOptions.row" /> </div> <div class="item" v-show="needShow('save', 'column')"> <label for="save-column">列</label> <input type="number" id="save-column" v-model.number="saveOptions.column" /> </div> <div class="item" v-show="needShow('save', 'rowCount')"> <label for="save-rowCount">行数</label> <input type="number" id="save-rowCount" v-model.number="saveOptions.rowCount" /> </div> <div class="item" v-show="needShow('save', 'columnCount')"> <label for="save-columnCount">列数</label> <input type="number" id="save-columnCount" v-model.number="saveOptions.columnCount" /> </div> </div> </div> </div> </div> </div> </template> <script setup> import GC from "@grapecity-software/spread-sheets"; import { ref, toRaw } from "vue"; import '@grapecity-software/spread-sheets-print'; import "@grapecity-software/spread-sheets-vue"; import "@grapecity-software/spread-sheets-io"; import '@grapecity-software/spread-sheets-shapes'; import '@grapecity-software/spread-sheets-charts'; import '@grapecity-software/spread-sheets-slicers'; import '@grapecity-software/spread-sheets-pivot-addon'; import '@grapecity-software/spread-sheets-reportsheet-addon'; import "@grapecity-software/spread-sheets-tablesheet"; import "@grapecity-software/spread-sheets-ganttsheet"; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); const openOptions = { openMode: 0, includeStyles: true, includeFormulas: true, frozenColumnsAsRowHeaders: false, frozenRowsAsColumnHeaders: false, fullRecalc: false, dynamicReferences: true, calcOnDemand: false, includeUnusedStyles: true, convertSheetTableToDataTable: false, incrementalLoading: false, encoding: "UTF-8", rowDelimiter: "\r\n", columnDelimiter: "," }; const saveOptions = { includeBindingSource: false, includeStyles: true, includeFormulas: true, saveAsView: false, rowHeadersAsFrozenColumns: false, columnHeadersAsFrozenRows: false, includeAutoMergedCells: false, includeCalcModelCache: false, saveR1C1Formula: false, includeUnusedNames: true, includeEmptyRegionCells: true, losslessEditing: true, encoding: "UTF-8", rowDelimiter: "\r\n", columnDelimiter: ",", sheetIndex: 0, row: 0, column: 0, rowCount: 200, columnCount: 20, }; const openOptionsConfig = { sjs: [{ propName: "openMode", type: "select", displayText: "OpenMode", options: [{ name: 'normal', value: 0 }, { name: 'lazy', value: 1 }, { name: 'incremental', value: 2 }], default: 0 }, { propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "fullRecalc", type: "boolean", default: false }, { propName: "dynamicReferences", type: "boolean", default: true }, { propName: "calcOnDemand", type: "boolean", default: false }, { propName: "includeUnusedStyles", type: "boolean", default: true },], xlsx: [{ propName: "openMode", type: "select", displayText: "OpenMode", options: [{ name: 'normal', value: 0 }, { name: 'lazy', value: 1 }, { name: 'incremental', value: 2 }], default: 0 }, { propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "frozenColumnsAsRowHeaders", type: "boolean", default: false }, { propName: "frozenRowsAsColumnHeaders", type: "boolean", default: false }, { propName: "fullRecalc", type: "boolean", default: false }, { propName: "dynamicReferences", type: "boolean", default: true }, { propName: "calcOnDemand", type: "boolean", default: false }, { propName: "includeUnusedStyles", type: "boolean", default: true }, { propName: "convertSheetTableToDataTable", type: "boolean", default: false }], ssjson: [{ propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "frozenColumnsAsRowHeaders", type: "boolean", default: false }, { propName: "frozenRowsAsColumnHeaders", type: "boolean", default: false }, { propName: "fullRecalc", type: "boolean", default: false }, { propName: "incrementalLoading", type: "boolean", default: false }], csv: [{ propName: "encoding", type: "string", default: "UTF-8" }, { propName: "rowDelimiter", type: "string", default: "\r\n" }, { propName: "columnDelimiter", type: "string", default: "," }] }; const saveOptionsConfig = { sjs: [{ propName: "includeBindingSource", type: "boolean", default: false }, { propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "saveAsView", type: "boolean", default: false }, { propName: "includeAutoMergedCells", type: "boolean", default: false }, { propName: "includeCalcModelCache", type: "boolean", default: false }, { propName: "saveR1C1Formula", type: "boolean", default: false }, { propName: "includeUnusedNames", type: "boolean", default: true }, { propName: "includeEmptyRegionCells", type: "boolean", default: true }, ], xlsx: [{ propName: "includeBindingSource", type: "boolean", default: false }, { propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "saveAsView", type: "boolean", default: false }, { propName: "rowHeadersAsFrozenColumns", type: "boolean", default: false }, { propName: "columnHeadersAsFrozenRows", type: "boolean", default: false }, { propName: "includeAutoMergedCells", type: "boolean", default: false }, { propName: "includeUnusedNames", type: "boolean", default: true }, { propName: "includeEmptyRegionCells", type: "boolean", default: true }, { propName: "losslessEditing", type: "boolean", default: true }, ], ssjson: [{ propName: "includeBindingSource", type: "boolean", default: false }, { propName: "includeStyles", type: "boolean", default: true }, { propName: "includeFormulas", type: "boolean", default: true }, { propName: "saveAsView", type: "boolean", default: false }, { propName: "rowHeadersAsFrozenColumns", type: "boolean", default: false }, { propName: "columnHeadersAsFrozenRows", type: "boolean", default: false }, { propName: "includeAutoMergedCells", type: "boolean", default: false }, ], csv: [{ propName: "encoding", type: "string", default: "UTF-8" }, { propName: "rowDelimiter", type: "string", default: "\r\n" }, { propName: "columnDelimiter", type: "string", default: "," }, { propName: "sheetIndex", type: "number", default: 0 }, { propName: "row", type: "number", default: 0 }, { propName: "column", type: "number", default: 0 }, { propName: "rowCount", type: "number", default: 200 }, { propName: "columnCount", type: "number", default: 20 }, ] }; const spreadRef = ref(null); const selectedFile = ref(null); const openFileType = ref(''); const saveFileType = ref('sjs'); const excelFileType = ref('XLSX'); const initSpread = function(spread) { spreadRef.value = spread; let statusBar = new GC.Spread.Sheets.StatusBar.StatusBar(document.getElementById('statusBar')); statusBar.bind(toRaw(spread)); } const selectedFileChange = function(e) { selectedFile.value = e.target.files[0]; openFileType.value = getFileType(selectedFile.value); } const open = function() { let spread = toRaw(spreadRef.value); let file = selectedFile.value; if (!file) { return; } let fileType = getFileType(file); let options = getOptions('open'); if (fileType === 'sjs') { spread.open(file, function() { }, function() { }, options); } else { spread.import(file, function() { }, function() { }, options); } } const save = function () { let spread = toRaw(spreadRef.value); let fileType = saveFileType.value; let fileName = 'export.' + (fileType==='xlsx' ? excelFileType.value.toLowerCase() : fileType); let options = getOptions('save'); if (fileType === 'sjs') { spread.save(function (blob) { saveAs(blob, fileName); }, function () { }, options); } else { if (fileType === 'csv') { var { sheetIndex, row, rowCount, column, columnCount } = options; options.range = { sheetIndex: sheetIndex, row: row, rowCount: rowCount, column: column, columnCount: columnCount, }; } options.fileType = mapExportFileType(fileType); spread.export(function (blob) { saveAs(blob, fileName); }, function () { }, options); } } const getOptions = function(mode) { let optionsConfig, optionsValue; if (mode === 'open') { optionsConfig = openOptionsConfig[openFileType.value]; optionsValue = openOptions; } else { optionsConfig = saveOptionsConfig[saveFileType.value]; optionsValue = saveOptions; } let options = {}; optionsConfig.forEach((prop) => { let v = optionsValue[prop.propName]; if (prop.type === 'number') { v = +v; } options[prop.propName] = v; }); if (mode === 'save' && saveFileType.value === 'xlsx') { options['excelFileType'] = excelFileType.value; } return options; } const getFileType = function(file) { if (!file) { return; } let fileName = file.name; let extensionName = fileName.substring(fileName.lastIndexOf(".") + 1); if (extensionName === 'sjs') { return 'sjs'; } else if (extensionName === 'xlsx' || extensionName === 'xlsm' || extensionName === 'xltm') { return 'xlsx'; } else if (extensionName === 'ssjson' || extensionName === 'json') { return 'ssjson'; } else if (extensionName === 'csv') { return 'csv'; } } const mapExportFileType = function(fileType) { if (fileType === 'ssjson') { return GC.Spread.Sheets.FileType.ssjson; } else if (fileType === 'csv') { return GC.Spread.Sheets.FileType.csv; } return GC.Spread.Sheets.FileType.excel; } const needShow = function(mode, propName) { let options = mode === 'open' ? openOptionsConfig[openFileType.value] : saveOptionsConfig[saveFileType.value]; return options && options.find((p) => p.propName === propName); } const showExcelFileType = () => saveFileType.value === 'xlsx'; </script> <style scoped> body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #app { height: 100%; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-container { width: calc(100% - 280px); height: 100%; float: left; } .sample-spreadsheets { width: 100%; height: calc(100% - 25px); overflow: hidden; } #statusBar { bottom: 0; height: 25px; width: 100%; position: relative; } .options-container { float: right; width: 280px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .sample-options { z-index: 1000; } .inputContainer { width: 100%; height: auto; border: 1px solid #eee; padding: 6px; margin-bottom: 10px; box-sizing: border-box; } .settingButton { color: #fff; background: #82bc00; outline: 0; line-height: 1.5715; position: relative; display: inline-block; font-weight: 400; white-space: nowrap; text-align: center; height: 32px; padding: 4px 15px; font-size: 14px; border-radius: 2px; user-select: none; cursor: pointer; border: 1px solid #82bc00; box-sizing: border-box; margin-bottom: 10px; margin-top: 10px; } .settingButton:hover { color: #fff; border-color: #88b031; background: #88b031; } .options-title { font-weight: bold; margin: 4px 2px; } #selectedFile { width: 180px; } #saveFileType { width: 120px; height: 31px; } .open-options .item { margin: 5px 0px; display: flex; } .save-options .item { margin: 5px 0px; display: flex; } label { margin-left: 3px; } select, input[type="text"], input[type="number"] { display: inline-block; margin-left: auto; width: 120px; font-weight: 400; outline: 0; line-height: 1.5715; border-radius: 2px; border: 1px solid #F4F8EB; box-sizing: border-box; } .excelFileTypeContainer { margin-bottom: 10px; } #saveExcelFileType { margin: 10px 0; width: 100%; } </style>
<!DOCTYPE html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/vue3/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="https://cdn.grapecity.com.cn/online/resources/compiler-sfc.esm-browser.js" type="module" defer></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/zh/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function(global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/', 'cdn:': 'https://cdn.grapecity.com.cn/SpreadJS/package-contents/19.0.0/' }, packageConfigPaths: [ './node_modules/*/package.json', "./node_modules/@grapecity-software/*/package.json", "./node_modules/@babel/*/package.json", "./node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', "systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js", '@grapecity-software/spread-sheets': 'cdn:spread-sheets/index.js', '@grapecity-software/spread-sheets-print': 'cdn:spread-sheets-print/index.js', '@grapecity-software/spread-sheets-vue': 'cdn:spread-sheets-vue/index.js', '@grapecity-software/spread-sheets-io': 'cdn:spread-sheets-io/index.js', '@grapecity-software/spread-sheets-charts': 'cdn:spread-sheets-charts/index.js', '@grapecity-software/spread-sheets-shapes': 'cdn:spread-sheets-shapes/index.js', '@grapecity-software/spread-sheets-slicers': 'cdn:spread-sheets-slicers/index.js', '@grapecity-software/spread-sheets-pivot-addon': 'cdn:spread-sheets-pivot-addon/index.js', '@grapecity-software/spread-sheets-reportsheet-addon': 'cdn:spread-sheets-reportsheet-addon/index.js', '@grapecity-software/spread-sheets-tablesheet': 'cdn:spread-sheets-tablesheet/index.js', '@grapecity-software/spread-sheets-ganttsheet': 'cdn:spread-sheets-ganttsheet/index.js', '@grapecity-software/spread-sheets-resources-zh': 'cdn:spread-sheets-resources-zh/index.js', }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);