只需3 分鐘,就能建立 一個SpreadJS 的 Vue 專案

77rou發表於2018-09-25

概述

SpreadJS純前端表格控制元件  V11.2(SP2)版本已經全面支援了Vue擴充,下面我們看下如何配合VUE CLI,只需3分鐘快速構建一個SpreadJS Vue工程。

安裝vue-cli(耗時30S)

通過命令```npm install -g @vue/cli ```安裝( cli.vuejs.org/

建立vue-spreadjs工程(耗時1Min)

請根據專案需求配置工程選項:

通過npm install 或者在package.json中新增引用的方式安裝spread.sheets(耗時20S)

"@grapecity/spread-excelio": "^11.2.3",
"@grapecity/spread-sheets": "^11.2.3",
"@grapecity/spread-sheets-print": "^11.2.3",
"@grapecity/spread-sheets-resources-zh": "^11.2.3",
"@grapecity/spread-sheets-vue": "^11.2.3",

修改router/index.js為spreadJS頁面新增router(耗時30S)

routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/spreadjs',
name: 'spreadJS',
component: SpreadJS
}
]

新建SpreadJS Component(耗時30S)

請在 components 下新增SpreadJS.vue檔案

template 內容:

<template>
<div>
<h1>Spread.Sheets</h1>
<div>
<input type='file' @change="processFile($event)"/>
<button @click="importExcel">匯入</button>
<button @click="exportExcel">匯出</button>
<button @click="printWorkbook">列印</button>
</div>
<div style="text-align: left">
<gc-spread-sheets
hostClass='spread-host'
@workbookInitialized = 'workbookInitialized($event)'>
<gc-worksheet>
</gc-worksheet>
</gc-spread-sheets>
</div>
</div>
</template>

Style內容:

<style>
.spread-host {
width: 100%;
height: 400px;
border: 1px solid black;
}
</style>

Script內容:

<script>
/* eslint-disable */
import "@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css";
import GC from "@grapecity/spread-sheets";
import "@grapecity/spread-sheets-vue";
import "@grapecity/spread-sheets-resources-zh";
import ExcelIO from "@grapecity/spread-excelio";
import FaverSaver from "file-saver";
import "@grapecity/spread-sheets-print";
GC.Spread.Common.CultureManager.culture("zh-cn");
GC.Spread.Sheets.LicenseKey = ExcelIO.LicenseKey = "your key"
export default {
methods: {
processFile (event) {
this.excelFile = event.target.files[0];
},
importExcel () {
var excelIO = new ExcelIO.IO();
console.log(excelIO);
var self = this;
excelIO.open(this.excelFile, function(json) {
self.spread.fromJSON(json);
console.log(json);
});
},
exportExcel () {
var excelIO = new ExcelIO.IO();
var json = this.spread.toJSON();
excelIO.save(
json,
function(blob) {
FaverSaver.saveAs(blob, "export.xlsx");
},
function(e) {
console.log(e);
}
);
},
printWorkbook (){
this.spread.print();
},
workbookInitialized(spread) {
this.spread = spread;
spread.refresh();
}
}
}
</script>

workbookInitialized是spread初始化完成後的回撥事件,我們可以在事件中得到初始化好的workbook物件。

部署授權需要同時給Sheets和ExcelIO同時新增,部署授權可以在全域性config中配置。

執行專案(耗時10S)

建立 npm install 依賴後,即可通過npm start啟動專案

瀏覽器訪問 http://localhost:8081/#/spreadjs 檢視效果。

只需3分鐘,一個 SpreadJS  的Vue專案就建立完成了,當然純前端表格控制元件SpreadJS的強大不僅於此,去實際試用感受一下吧

擴充套件閱讀

這篇文章,講述的是《 3分鐘建立 SpreadJS 的 React 專案 》,需要的同學請深入瞭解。

純前端表格控制元件SpreadJS ,是市面上佈局與功能都與 Excel 高度類似的一款表格控制元件,全中文操作介面,適用於.NET、Java、移動端等多個平臺的類 Excel 資料開發,備受華為、海信、立信、中國平安、中國能建、中通快遞、金麒麟和北京神軟等客戶青睞。

關於 葡萄城

賦能開發者!葡萄城公司成立於 1980 年,是全球領先的集開發工具、商業智慧解決方案、管理系統設計工具於一身的軟體和服務提供商。西安葡萄城是其在中國的分支機構,面向全球市場提供軟體研發服務,併為中國企業的資訊化提供國際先進的開發工具、軟體和研發諮詢服務。葡萄城的控制元件和軟體產品在國內外屢獲殊榮,在全球被數十萬家企業、學校和政府機構廣泛應用。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28298702/viewspace-2214777/,如需轉載,請註明出處,否則將追究法律責任。

相關文章