当前位置: 首页 > news >正文

前端 Excel 工具组件实战:导入 → 可编辑表格 → 导出 + 样式同步 + 单元格合并

目录

  • 1. 项目初始化与依赖安装
  • 2. 表格组件设计(基于 Handsontable)
  • 3. 导入功能:支持 `.xlsx` / `.csv`
  • 4. 导出功能:带样式 & 合并信息
  • 5. 样式同步实现方案
  • 6. 合并单元格功能逻辑
  • 7. 拓展功能建议 + 示例代码
    • ✅ 设置单元格背景色按钮
    • ✅ 支持 JSON 数据导出保存

1. 项目初始化与依赖安装

npm init vite@latest excel-editor --template vue-ts  
cd excel-editor  
npm install  
npm install xlsx file-saver handsontable @handsontable/vue  
npm install -D tailwindcss postcss autoprefixer  
npx tailwindcss init -p  

main.ts 中引入 Tailwind:

import './index.css'  

2. 表格组件设计(基于 Handsontable)

我们封装一个 ExcelEditor.vue 组件,使用 @handsontable/vue

<template>  <hot-table  ref="hotTableRef"  :settings="settings"  licenseKey="non-commercial-and-evaluation"  />  
</template>  <script setup lang="ts">  
import { ref } from 'vue'  
import { HotTable } from '@handsontable/vue'  
import Handsontable from 'handsontable'  const hotTableRef = ref()  
const settings = {  data: Handsontable.helper.createEmptySpreadsheetData(20, 10),  rowHeaders: true,  colHeaders: true,  contextMenu: true,  mergeCells: [],  manualRowResize: true,  manualColumnResize: true,  comments: true,  cells(row: number, col: number) {  return { className: '' }  }  
}  
</script>  

3. 导入功能:支持 .xlsx / .csv

import * as XLSX from 'xlsx'  const handleFileUpload = (file: File) => {  const reader = new FileReader()  reader.onload = (e) => {  const data = new Uint8Array(e.target!.result as ArrayBuffer)  const workbook = XLSX.read(data, { type: 'array' })  const sheet = workbook.Sheets[workbook.SheetNames[0]]  const json = XLSX.utils.sheet_to_json(sheet, { header: 1 })  hotTableRef.value.hotInstance.loadData(json)  }  reader.readAsArrayBuffer(file)  
}  

4. 导出功能:带样式 & 合并信息

import { saveAs } from 'file-saver'  const exportToExcel = () => {  const hot = hotTableRef.value.hotInstance  const data = hot.getData()  const ws = XLSX.utils.aoa_to_sheet(data)  const merges = hot.getPlugin('mergeCells').mergedCellsCollection.mergedCells  ws['!merges'] = merges.map(m => ({  s: { r: m.row, c: m.col },  e: { r: m.row + m.rowspan - 1, c: m.col + m.colspan - 1 }  }))  const wb = XLSX.utils.book_new()  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')  const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })  saveAs(new Blob([wbout]), 'table.xlsx')  
}  

5. 样式同步实现方案

const cellStyles = new Map<string, any>()  const updateCellStyle = (row: number, col: number, style: any) => {  const key = `${row}_${col}`  cellStyles.set(key, style)  
}  cells(row, col) {  const key = `${row}_${col}`  const style = cellStyles.get(key) || {}  return {  renderer(hotInstance, td, row, col, prop, value) {  td.style.backgroundColor = style.backgroundColor || ''  td.style.fontWeight = style.fontWeight || ''  td.innerText = value  return td  }  }  
}  

6. 合并单元格功能逻辑

const mergeSelectedCells = () => {  const hot = hotTableRef.value.hotInstance  const selected = hot.getSelectedLast()  if (!selected) return  const [startRow, startCol, endRow, endCol] = selected  hot.getPlugin('mergeCells').merge({  row: Math.min(startRow, endRow),  col: Math.min(startCol, endCol),  rowspan: Math.abs(endRow - startRow) + 1,  colspan: Math.abs(endCol - startCol) + 1,  })  hot.render()  
}  

7. 拓展功能建议 + 示例代码

✅ 设置单元格背景色按钮

<button @click="setCurrentCellStyle({ backgroundColor: '#ffff99' })">  高亮当前单元格  
</button>  const setCurrentCellStyle = (style: any) => {  const hot = hotTableRef.value.hotInstance  const [row, col] = hot.getSelectedLast() || []  updateCellStyle(row, col, style)  hot.render()  

✅ 支持 JSON 数据导出保存

const getJsonData = () => {  const data = hotTableRef.value.hotInstance.getData()  return JSON.stringify(data)  
}  

到这里,这篇文章就和大家说再见啦!我的主页里还藏着很多 篇 前端 实战干货,感兴趣的话可以点击头像看看,说不定能找到你需要的解决方案~
创作这篇内容花了很多的功夫。如果它帮你解决了问题,或者带来了启发,欢迎:
点个赞❤️ 让更多人看到优质内容
关注「前端极客探险家」🚀 每周解锁新技巧
收藏文章⭐️ 方便随时查阅
📢 特别提醒:
转载请注明原文链接,商业合作请私信联系
感谢你的阅读!我们下篇文章再见~ 💕

在这里插入图片描述

相关文章:

  • 机器人灵巧手有刷与无刷空心杯电机解析
  • Kafka 命令行操作与 Spark-Streaming 核心编程总结
  • 让Docker端口映射受Firewall管理而非iptables
  • Python爬虫爬取图片并存储到MongoDB(注意:仅尝试存储一条空的示例数据到MongoDB,验证MongoDB的联通性)
  • Vue3 setup、计算属性、侦听器、响应式API
  • 【go语言】window环境从源码编译go
  • 游戏引擎学习第241天:将OpenGL VSync 和 sRGB 扩展
  • 【c++】【STL库】vector类详解
  • Unity 使用 ADB 实时查看手机运行性能
  • [linux]设置邮件发送告警功能
  • 【C++】入门基础【下】
  • 编译 C++ 报错“找不到 g++ 编译器”的终极解决方案(含 Windows/Linux/macOS)
  • 2025最新系统 Linux 教程(六)
  • HTML5 服务器发送事件 (Server-Sent Events):实现网页自动获取服务器更新
  • 第53.5讲 | 小项目实战:用 SHAP 值解释农作物产量预测模型 [特殊字符][特殊字符]
  • Next.js v15 eslint 规则配置
  • Spring Boot知识点详解
  • 27、Session有什么重⼤BUG?微软提出了什么⽅法加以解决?
  • 【基础】Node.js 介绍、安装及npm 和 npx功能了解
  • 如何快速高效学习Python?
  • 铁路上海站五一假期预计发送446万人次,同比增长8.4%
  • 利用AI捏造“天价骨灰盒”谣言,内蒙古包头一网民被行政处罚
  • 饶权已任国家文物局局长
  • 我驻美使馆:中美并没有就关税问题磋商谈判,更谈不上达成协议
  • “归雁经济”能带来什么?川大商学院调研团队深入乡村与返乡青年人才交流
  • 美称中美贸易谈判仍在进行中,外交部:美方不要混淆视听