Vue+SpringBoot+SpreadJS 實現的在線文檔

在線文檔,顧名思義就是通過在線的方式對文檔進行操作,實現如數據填報、數據計算、可視化、在線導入導出 Excel 文件、自定義系統外觀、工具欄、在網頁內滿足單人或多人編輯並將文件上傳至服務端保存的功能。

國內外常見的在線文檔,包括:微軟Office Online、Google文檔、石墨文檔、騰訊文檔、飛書、語雀等。點擊此處,瞭解這些軟件產品的功能對比

image.png

通過對比這些軟件產品,可以將它們的主要功能點,概括爲三個層面:數據填報、協同編輯和類Excel的呈現方式。

· 數據填報:支持在線導入導出 Excel、大數據量填報、填報暫存、多 sheet 填報、多級上報

· 協同編輯:支持多人協作、實時編輯、數據同步、多級上報、歷史查詢

· 類 Excel的呈現方式:提供類Excel的操作模式和使用體驗,兼容Excel數據結構

可以說,只要滿足了上述需求,就可以開發出一款優秀的在線文檔。

而SpreadJS純前端表格控件,恰好滿足。

image.png

下面,我們就通過代碼,演示如何藉助 SpreadJS 搭建一款簡單的在線文檔系統。

在線文檔系統截圖

該系統集成了SpreadJS表格控件,目前已經實現了在線讀取、編輯Excel 文檔的功能,文末提供了示例代碼下載,大家可以按照 Readme文檔中的步驟運行。

PS:歡迎大家前往SpreadJS 產品官網,瞭解並學習這款純前端表格組件,以實現更多在線文檔的功能。

如下是系統運行截圖。

進入頁面(File List的展示文件均爲服務器上的文件):

image.png

單擊 Excel文件,右側會顯示該文件的詳細信息:

image.png

雙擊文件夾:

image.png

點擊某個 Excel文件,打開編輯文件的彈框:

image.png

在線對Excel文件進行修改、刪除:

image.png

技術棧

· 後端:SpringBoot

· 前端:SpreadJS、Vue、ElementUI、Typescript

關鍵步驟

實現該系統的相關依賴包都寫在package.json裏,執行命令 npm install 即可安裝,主要有:

 "@grapecity/spread-sheets": "^13.0.5",

 "@grapecity/spread-sheets-resources-zh": "^13.0.5",

 "@grapecity/spread-sheets-vue": "^13.0.5",

 "@grapecity/spread-sheets-pdf": "^13.0.5",

 "@grapecity/spread-sheets-print": "^13.0.5",

 "@grapecity/spread-sheets-charts": "^13.0.5",

 "@grapecity/spread-sheets-shapes": "^13.0.5",

在 SpreadJS.vue文件中引入 SpreadJS 相關安裝包:

 import "@grapecity/spread-sheets-vue";

 import * as GC from "@grapecity/spread-sheets";

 import "@grapecity/spread-sheets-charts";

 import "@grapecity/spread-sheets-shapes";

在App.vue文件中引入 SpreadJS 的樣式文件、中文資源文件:

 import '@grapecity/spread-sheets-resources-zh'

 import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css'

List.vue文件中是主要的頁面邏輯,雙擊文件時彈出彈框,彈框中引入了自定義的組件:

 <MySpreadJS :mySpread="mySpread" :filePath="filePath" @done="childDone" :closeSpread='closeSpread'></MySpreadJS>

mySpread變量中是從後臺返回的文件流,傳給MySpreadJS 組件,子組件接收到數據後,調用excelIO.open方法打開Excel 文件流,

  spreadInitHandle: function(spread) {

 this.spread = spread;

 let self = this;

 let excelIO = new ExcelIO.IO();

 excelIO.open(

 this.spreadblob as Blob,

 function(json) {

 let workbookObj = json;

 self.spread.fromJSON(workbookObj);

 },

 function(e) {

 alert(e.errorMessage);});},

在線編輯Excel文件,並保存。藉助excelIO.save方法可以將修改後的文件流傳給後臺,實現文件的修改功能:

excelIO.save(

 curjson,

 function(fileblob) {

 let formData = new FormData();

 formData.append("filePath", self.filePath);

 formData.append("type", "update");

 formData.append("excelFile", fileblob);

 httpUtils.post("/filemanager/savefile", formData).then(response => {

 self.$message({

 type: "success",

 message: "保存成功!"

 });

 self.$emit("done");

 });},

 function(e) {

 //process error

 console.log(e);});

後臺接口介紹

獲取某路徑下的文件集合:getfolder

image.png

獲取 Excel文件內容:getFile

image.png

保存文件: savefile

image.png

刪除文件: deletefile

image.png


以上就是Vue+SpringBoot+SpreadJS 實現的一個簡單的在線文檔,如需瞭解詳細的實現步驟,請下載示例代碼

SpreadJS除提供示例中在線讀取、編輯Excel 文檔的功能外,還提供了Web Excel 組件開發、數據填報、Excel 類報表設計等功能。除此之外,您還可通過調用API對SpreadJS進一步擴展,滿足協同編輯、多級上報、填報暫存等更多場景的業務需要,歡迎前往SpreadJS 產品官網,下載試用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章