Vue2+Univer 环境搭建

node js 版本 16.32+

参考文档:

Vue2+Univer实现可编辑Excel_vue univer-CSDN博客

https://univer.ai/guides/sheet/getting-started/quickstart

实现步骤:

1、包里面直接写这些

"@riophae/vue-treeselect": "0.4.0",
    "@univerjs/core": "0.2.4",
    "@univerjs/design": "0.2.4",
    "@univerjs/docs": "0.2.4",
    "@univerjs/docs-ui": "0.2.4",
    "@univerjs/engine-formula": "0.2.4",
    "@univerjs/engine-render": "0.2.4",
    "@univerjs/facade": "^0.2.4",
    "@univerjs/network": "0.2.4",
    "@univerjs/rpc": "0.2.4",
    "@univerjs/sheets": "0.2.4",
    "@univerjs/sheets-formula": "0.2.4",
    "@univerjs/sheets-numfmt": "0.2.4",
    "@univerjs/sheets-ui": "0.2.4",
    "@univerjs/ui": "0.2.4",

这个的数据来源官网的这里,拷贝过来

StackBlitz

package里面的这个依赖也是

  "devDependencies": {
    "@univerjs/webpack-plugin": "0.4.3",

然后vue.config里面加个:new UniverPlugin()

  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
      new CompressionPlugin({
        cache: false,                                  // 不启用文件缓存
        test: /\.(js|css|html|jpe?g|png|gif|svg)?$/i,  // 压缩文件格式
        filename: '[path][base].gz[query]',            // 压缩后的文件名
        algorithm: 'gzip',                             // 使用gzip压缩
        minRatio: 0.8,                                 // 压缩比例,小于 80% 的文件不会被压缩
        deleteOriginalAssets: false                    // 压缩后删除原文件
      }),new UniverPlugin()
    ],
  },

新建一个universheet页面

<template>
  <div ref="container"
       class="univer-container"></div>
</template>

<script>
import { Univer, UniverInstanceType, LocaleType } from '@univerjs/core'
import { defaultTheme } from '@univerjs/design'
import { UniverDocsPlugin } from '@univerjs/docs'
import { UniverDocsUIPlugin } from '@univerjs/docs-ui'
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'
import { UniverRenderEnginePlugin } from '@univerjs/engine-render'
import { UniverSheetsPlugin } from '@univerjs/sheets'
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula'
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui'
import { UniverUIPlugin } from '@univerjs/ui'
import { FUniver  } from '@univerjs/facade'
import { zhCN, enUS } from 'univer:locales'
import '@univerjs/sheets-ui/lib/index.css'
export default {
  name: 'UniverSheet',
  props: {
    data: {
      value: Object,
      default: () => ({})
    }
  },
  watch: {
    data: {
      handler(val) {
        this.destroyUniver()
        this.init(val)
      },
      immediate: true
    }
  },
  data() {
    return {
      univer: null,
      workbook: null,
      univerAPI: null
    }
  },
  mounted() {
    this.init(this.data)
  },
  methods: {
    init(data = {}) {
      const univer = new Univer({
        theme: defaultTheme,
        locale: LocaleType.ZH_CN,
        locales: {
          [LocaleType.ZH_CN]: zhCN,
          [LocaleType.EN_US]: enUS
        }
      })
      this.univer = univer
      univer.registerPlugin(UniverRenderEnginePlugin)
      univer.registerPlugin(UniverFormulaEnginePlugin)
      univer.registerPlugin(UniverUIPlugin, {
        container: this.$refs.container
      })

      univer.registerPlugin(UniverDocsPlugin, {
        hasScroll: false
      })
      univer.registerPlugin(UniverDocsUIPlugin)
      univer.registerPlugin(UniverSheetsPlugin)
      univer.registerPlugin(UniverSheetsUIPlugin)
      univer.registerPlugin(UniverSheetsFormulaPlugin)

      this.workbook = univer.createUnit(UniverInstanceType.UNIVER_SHEET, data)

      this.univerAPI = FUniver.newAPI(univer)
    },
    destroyUniver() {
      this.univer?.dispose()
      this.univer = null
      this.workbook = null
    },
    getData() {
      if (!this.workbook) {
        throw new Error('未初始化')
      }
      return this.workbook.save()
    }
  }
}
</script>
<style scoped>
.univer-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
:global(.univer-menubar) {
  display: none;
}
</style>

然后再来个页面引用他

<template>
   <div style="height: 800px;width: 100%;">
   <button @click="getData">获取数据</button>
   <UniverSheet id="sheet"
                ref="univerRef"
                :data="data" />
 </div>
</template>

<script>

import UniverSheet from './UniverSheet.vue'
export default {
  name: "Index",
  components:{UniverSheet},
  data(){
    return{
      select:[],
      data: {},
      loginUser: ''
    }
  },
  mounted(){

  },
  methods:{
    getData() {
      const result = this.$refs.univerRef?.getData()
      console.log(JSON.stringify(result, null, 2))
    },

  }
}
</script>

<style lang="less" scoped>


}
</style>

启动

相关推荐
CoderJia程序员甲5 分钟前
重学SpringBoot3-Spring WebFlux之HttpHandler和HttpServer
java·spring boot·reactor·1024程序员节
长潇若雪1 小时前
指针进阶(四)(C 语言)
c语言·开发语言·经验分享·1024程序员节
混迹网络的权某2 小时前
每天一道C语言精选编程题之求数字的每⼀位之和
c语言·开发语言·考研·算法·改行学it·1024程序员节
IronmanJay5 小时前
【LeetCode每日一题】——862.和至少为 K 的最短子数组
数据结构·算法·leetcode·前缀和·双端队列·1024程序员节·和至少为 k 的最短子数组
加载中loading...6 小时前
Linux线程安全(二)条件变量实现线程同步
linux·运维·服务器·c语言·1024程序员节
Wx120不知道取啥名6 小时前
C语言之长整型有符号数与短整型有符号数转换
c语言·开发语言·单片机·mcu·算法·1024程序员节
biomooc7 小时前
R语言 | paletteer包:拥有2100多个调色板!
r语言·数据可视化·1024程序员节
Hello.Reader7 小时前
FFmpeg 深度教程音视频处理的终极工具
ffmpeg·1024程序员节
Y.O.U..8 小时前
STL学习-容器适配器
开发语言·c++·学习·stl·1024程序员节
就爱敲代码8 小时前
怎么理解ES6 Proxy
1024程序员节