vue3+luckyexcel+php在线编辑excel文件

开发过程中,需要开发一个在线编辑excel文档的功能,找到了这个合适的组件

Luckysheet ,一款纯前端类似excel的在线表格,功能强大、配置简单、完全开源。

可以导入文档,预览、编辑、保存、导出等功能,可以满足大部分需求

第一步:需要先安装 vue3 运行下面三个安装命令

npm install exceljs -S 
npm install luckyexcel -S
npm install file-saver

第二步:前端部分index.html 加入引用代码

<link rel='stylesheet' href='/luckysheet/pluginsCss.css' /><link rel='stylesheet' href='/luckysheet/plugins.css' /><link rel='stylesheet' href='/luckysheet/luckysheet.css' /><link rel='stylesheet' href='/luckysheet/iconfont.css' /><script src="/luckysheet/plugin.js"></script><script src="/luckysheet/luckysheet.umd.js"></script>

组件部分test.vue

<template><div style="position: absolute; top: 0"><input id="uploadBtn" type="file" @change="loadExcel" /><button class="btn" @click="getExcel">后台数据</button><span>Or文件:</span><select v-model="selected" @change="selectExcel"><option disabled value="">Choose</option><option v-for="option in options" :key="option.text" :value="option.value">{{ option.text }}</option></select><input class="inp" type="text" v-model="excelTitel"><button class="blueBtn" @click="editClicked">编辑</button><button class="btn" @click="saveExcel">保存</button><a href="javascript:void(0)" @click="downloadExcel">下载</a></div><div id="luckysheet"></div><div v-show="isMaskShow" id="tip">Downloading</div>
</template>
test.vue   script代码部分
import { ref, onMounted } from 'vue'
import http from '@/assets/js/procure-http.js'
import { exportExcel } from '@/components/export'
import LuckyExcel from 'luckyexcel'const isMaskShow = ref(false)
const selected = ref('')
const jsonData = ref({})
const excelTitel = ref('')
const congifdata = ref({container: 'luckysheet',title: "bi", // 工作簿名称lang: "zh", // 设定表格语言 国际化设置,允许设置表格的语言,支持中文("zh")和英文("en")allowCopy: false, // 是否允许拷贝showtoolbar: false, // 是否显示工具栏showinfobar: true, // 是否显示顶部信息栏showsheetbar: false, // 是否显示底部sheet页按钮showstatisticBar: false, // 是否显示底部计数栏sheetBottomConfig: false, // sheet页下方的添加行按钮和回到顶部按钮配置allowEdit: false, // 是否允许前台编辑enableAddRow: false, // 允许增加行enableAddCol: false, // 允许增加列userInfo: false, // 右上角的用户信息展示样式showRowBar: false, // 是否显示行号区域showColumnBar: false, // 是否显示列号区域sheetFormulaBar: false, // 是否显示公式栏enableAddBackTop: false,//返回头部按钮rowHeaderWidth: 0,//纵坐标columnHeaderHeight: 0,//横坐标showstatisticBarConfig: {count:false,view:false,zoom:false,},showsheetbarConfig: {add: false, //新增sheetmenu: false, //sheet管理菜单sheet: false, //sheet页显示},forceCalculation: true,//强制计算公式
})
const options = ref([{ text: 'Money Manager.xlsx', value: 'https://xxxxxx/storage/salarytemp/20231222/20231222162622.xlsx' },{text: 'Activity costs tracker.xlsx', value: 'https://xxxxxx/storage/salary/20231031/0f724adf33a2d3d0b95071b0c52fb711.xlsx'}
])const loadExcel = (evt) => {const files = evt.target.filesif (files == null || files.length == 0) {alert('No files wait for import')return}let name = files[0].namelet suffixArr = name.split('.'),suffix = suffixArr[suffixArr.length - 1]if (suffix != 'xlsx') {alert('Currently only supports the import of xlsx files')return}LuckyExcel.transformExcelToLucky(files[0], function (exportJson, luckysheetfile) {if (exportJson.sheets == null || exportJson.sheets.length == 0) {alert('Failed to read the content of the excel file, currently does not support xls files!')return}console.log('exportJson', exportJson)jsonData.value = exportJsonconsole.log(exportJson.sheets)window.luckysheet.destroy()excelTitel.value = exportJson.info.namecongifdata.value.data = exportJson.sheetscongifdata.value.title = exportJson.info.namecongifdata.value.userInfo = exportJson.info.name.creatorwindow.luckysheet.create(congifdata.value)})
}
const selectExcel = (evt) => {const value = selected.valueconst name = evt.target.options[evt.target.selectedIndex].innerTextif (value == '') {return}isMaskShow.value = trueLuckyExcel.transformExcelToLuckyByUrl(value, name, (exportJson, luckysheetfile) => {if (exportJson.sheets == null || exportJson.sheets.length == 0) {alert('Failed to read the content of the excel file, currently does not support xls files!')return}console.log('exportJson', exportJson)jsonData.value = exportJsonisMaskShow.value = falsewindow.luckysheet.destroy()window.luckysheet.create({container: 'luckysheet', //luckysheet is the container idshowinfobar: false,data: exportJson.sheets,title: exportJson.info.name,userInfo: exportJson.info.name.creator})})
}
// 导出
const downloadExcel = () => {exportExcel(luckysheet.getAllSheets(), excelTitel.value)
}
// 从后台获取数据
const getExcel = () => {http.get('/index/index', {}, res => {if(res.code == 200) {window.luckysheet.destroy()console.log(JSON.parse(res.data))congifdata.value.data = JSON.parse(res.data)congifdata.value.title = '测试'window.luckysheet.create(congifdata.value)}})
}
// 保存excel数据
const saveExcel = () => {var excel = luckysheet.getAllSheets();//去除临时数据,减小体积for(var i in excel)excel[i].data = undefined// console.log(JSON.stringify(data))http.post('/index/update', {data:JSON.stringify(excel)}, res => {console.log(res)if(res.code == 200) {}})
}
const editClicked = () =>{congifdata.value.showtoolbar = truecongifdata.value.allowEdit = trueluckysheet.create(congifdata.value)
}
// !!! create luckysheet after mounted
onMounted(() => {luckysheet.create(congifdata.value)
})
</script><style scoped>
#luckysheet {margin: 0px;padding: 0px;position: absolute;width: 100%;left: 0px;top: 30px;bottom: 0px;height:900px;
}#uploadBtn {font-size: 16px;
}#tip {position: absolute;z-index: 1000000;left: 0px;top: 0px;bottom: 0px;right: 0px;background: rgba(255, 255, 255, 0.8);text-align: center;font-size: 40px;align-items: center;justify-content: center;display: flex;
}
</style>

运行后效果如图

本地引用文件的需要下载好组件

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/305036.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

实战 | 使用OpenCV快速去除文档中的表格线条(步骤 + 源码)

导 读 本文主要介绍如何使用OpenCV快速去除文档中的表格线条,并给详细步骤和代码。 背景介绍 测试图如下,目标是去除下面三张图中的表格线条,方便后续图像处理。 实现步骤 下面演示详细步骤,以图1为例: 【1】获取二值图像:加载图像、转为灰度图、OTSU二值化 i…

echarts 柱状图

记录echarts 柱状图基础案例以及相关配置。 1.基础柱状图 const myChart this.$echarts.init(this.$refs.echartsZx);const option {title: {text: 本周考试记录},//提示框tooltip: {trigger: axis,axisPointer: {type: shadow}},xAxis: {type: category,data: [Mon, Tue, W…

IDEA、VSCode等快速连接Github(Mac版)

问题描述 在本地书写✍️完代码后, 想要git push到Github上面, 出现延迟错误; 导致经常push不上去, 如下图所示; 解决方案 进入电脑终端; 输入下列命令; sudo vim /etc/hosts输入密码; 按下 I 键, 进行编辑操作; 将下列语句复制到空白区, 然后按下esc按键, 然后输入:wq即可…

Qt Creator可视化交互界面exe快速入门4

上一期介绍了信号与槽&#xff0c;本期介绍加法计算器 我们来新建一个项目 然后拖动设置按钮 还需要个输出框 这里拖动Line Edit 我这里只是简单演示一下&#xff0c;做个低配版计算器&#xff0c;再加个加号和一个等于号就结束了。 然后回到代码编辑部分&#xff0c;我们需要…

mysql8 linux安装过程(通用版)

下载&#xff1a;通用版&#xff0c;建议用tar.gz包 下载地址&#xff1a;https://downloads.mysql.com/archives/community/ 注意选项&#xff1a;选linux-Generic以及架构和位数 Operating System:Linux-Generic; OS Version:Linux-Generic(glibc 2.28)(x86,64-bit) 安装过…

制作gif动图软件,视频转gif动图生成器

生活在这个快节奏的时代&#xff0c;我们总是希望能够抓住那些转瞬即逝的美好。而gif动图&#xff0c;正是这样一种能够让时间静止、让美好定格的存在。从视频到gif动图&#xff0c;不仅仅是格式的转换&#xff0c;更是情感的传递与分享。 所需工具&#xff1a; 一个【媒体梦…

Android : 画布绘制矩形和文字 让其居中显示简单应用

示例图&#xff1a; CenterView.java package com.example.demo;import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.View;public class Center…

哪种猫粮比较好?怎样囤性价比高的主食冻干品牌 ?

在过去的100多年里&#xff0c;猫咪主食市场一直被膨化猫粮主导。然而&#xff0c;随着猫咪频频出现猝死、失明、发育不良以及营养不良等问题&#xff0c;猫主人们开始质疑膨化粮是否最适合猫咪。于是&#xff0c;从上世纪90年代开始&#xff0c;出现了生骨肉喂养。生骨肉确实是…

腾讯云轻量应用服务器和云服务器性能一样吗?安全吗?

腾讯云轻量服务器和云服务器CVM该怎么选&#xff1f;不差钱选云服务器CVM&#xff0c;追求性价比选择轻量应用服务器&#xff0c;轻量真优惠呀&#xff0c;活动 https://curl.qcloud.com/oRMoSucP 轻量应用服务器2核2G3M价格62元一年、2核2G4M价格118元一年&#xff0c;540元三…

Redis 核心知识总结

Redis 核心知识总结 认识 Redis 什么是 Redis&#xff1f; Redis 是一个由 C 语言开发并且基于内存的键值型数据库&#xff0c;对数据的读写操作都是在内存中完成&#xff0c;因此读写速度非常快&#xff0c;常用于缓存&#xff0c;消息队列、分布式锁等场景。 有以下几个特…

Buck电源设计常见的一些问题(四)MOS管振荡抑制方法(二)

MOS管振荡抑制方法(二)RC snubber 缓冲电路的设计 1. Snubber 电路2.开关回路等效电路3. RC参数设计1. Snubber 电路 由于寄生参数的存在,开关电源电路在开关动作瞬间会产生开关振铃。图 1 为 buck 电路开关节点 (两个开关与电感交汇点)的典型波形,可见在上管开通瞬间都…

[枚举涂块]画家问题

画家问题 题目描述 有一个正方形的墙&#xff0c;由N*N个正方形的砖组成&#xff0c;其中一些砖是白色的&#xff0c;另外一些砖是黄色的。Bob是个画家&#xff0c;想把全部的砖都涂成黄色。但他的画笔不好使。当他用画笔涂画第(i, j)个位置的砖时&#xff0c; 位置(i-1, j)、…