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

建设信用卡激活中心网站一个专门做特产的网站

建设信用卡激活中心网站,一个专门做特产的网站,wordpress 会议 主题,网站建设 中国联盟网在现代 web 开发中,数据导入是一个常见的需求,而 Excel 文件作为数据存储的重要格式之一,能够实现 Excel 表格的解析并展示在前端页面上是一项非常实用的功能。本文将详细介绍如何在 Vue 3 项目中使用 ExcelJS 库来解析 Excel 文件&#xff0…

在现代 web 开发中,数据导入是一个常见的需求,而 Excel 文件作为数据存储的重要格式之一,能够实现 Excel 表格的解析并展示在前端页面上是一项非常实用的功能。本文将详细介绍如何在 Vue 3 项目中使用 ExcelJS 库来解析 Excel 文件,并将数据展示在表格中。

安装依赖库

npm install exceljs

代码实现

模板部分

<template><div class="addBatchTask-func"><div class="title"><h2>Excel 表格解析 Demo</h2></div><div class="but"><a-button @click="handleUpload" type="primary">上传表格</a-button><inputtype="file"ref="fileInput"style="display: none"@change="handleFileChange"accept=".xlsx"/></div><div class="table-box" v-if="data.length > 0"><a-table :columns="columns" :data-source="data" :pagination="false"><template #bodyCell="{ column, record }"><a-buttontype="primary"@click="handleOk(record)"v-if="column.key === 'action'">删除</a-button></template></a-table></div></div>
</template>
<style lang="less" scoped>
.addBatchTask-func {width: 100%;height: 100vh;.table-box {:deep(.ant-table) {background: black;.ant-table-thead {.ant-table-cell {background: #000;color: #fff;font-size: 14px;font-weight: 600;}}}}
}
</style>

 脚本部分

 

<script setup>
import * as ExcelJS from "exceljs";
import { ref } from "vue";const fileInput = ref(null);const data = ref([]);const columns = [{title: "姓名",dataIndex: "name",key: "name",},{title: "年龄",dataIndex: "age",key: "age",},{title: "性别",dataIndex: "sex",key: "sex",},{title: "家庭住址",dataIndex: "address",key: "address",},{title: "操作",key: "action",},
];// 处理上传文件的事件
const handleUpload = () => {const fileInput = document.querySelector('input[type="file"]');console.log(fileInput, "fileInput");fileInput.value = ""; // 重置文件输入字段的值fileInput.click();
};const handleFileChange = async (event) => {data.value = [];const file = event.target.files[0];console.log(file, "file");if (!file) return;try {const reader = new FileReader();reader.onload = async (e) => {if (!e.target || !e.target.result) {reject("File reading failed");return;}const arrayBuffer = e.target.result;const workbook = new ExcelJS.Workbook();await workbook.xlsx.load(arrayBuffer);const worksheet = workbook.getWorksheet(1);worksheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {if (rowNumber > 1) {const rowData = {key: rowNumber.toString(),name: row.getCell(1).value || "",age: row.getCell(2).value || "",sex: row.getCell(3).value || "",address: row.getCell(4).value || "",};console.log(rowData, "rowData");data.value.push(rowData);}});};reader.onerror = (error) => {console.log(error);};reader.readAsArrayBuffer(file);} catch (error) {console.error("Error importing table:", error);}
};
</script>

注意事项和优化建议

  1. 文件格式支持:目前只支持 .xlsx 格式,可以根据需求扩展支持 .xls 等其他格式。

  2. 数据验证和处理:在解析和展示数据时,应考虑添加数据验证逻辑,确保数据的完整性和正确性。

  3. 性能优化:如果处理的 Excel 文件较大,可以考虑分批读取和展示数据,以提高性能。

  4. 错误处理:增加更多的错误处理逻辑,提高用户体验。


文章转载自:

http://EeTMWI2O.xhfpg.cn
http://a6BpZe4O.xhfpg.cn
http://U7RNO04x.xhfpg.cn
http://4rHRpxAj.xhfpg.cn
http://9xXxEpZj.xhfpg.cn
http://bzkAcE3h.xhfpg.cn
http://PEyJXXmO.xhfpg.cn
http://xmRwxgc5.xhfpg.cn
http://CCXaRIBE.xhfpg.cn
http://nJWM4Zzs.xhfpg.cn
http://yYZF9oMo.xhfpg.cn
http://bSt34h5n.xhfpg.cn
http://JqFaBTj8.xhfpg.cn
http://ywGNfTHQ.xhfpg.cn
http://plSHcpbH.xhfpg.cn
http://vvjFXUc0.xhfpg.cn
http://5dBPFwkI.xhfpg.cn
http://8DhEPptU.xhfpg.cn
http://myQeMiu9.xhfpg.cn
http://gtQXrE2P.xhfpg.cn
http://R9IiozBa.xhfpg.cn
http://83H86Swh.xhfpg.cn
http://9lH0j4Ar.xhfpg.cn
http://NpoVzcM8.xhfpg.cn
http://xozUNn0D.xhfpg.cn
http://qWRvcZYg.xhfpg.cn
http://02WgOAKk.xhfpg.cn
http://Pu6rOMtX.xhfpg.cn
http://wGQVRynq.xhfpg.cn
http://XDtga65n.xhfpg.cn
http://www.dtcms.com/wzjs/639179.html

相关文章:

  • 网站推广优化外包公司wordpress 手机菜单栏插件
  • c 能做网站网站如何改首页模块
  • 深圳罗湖区网站南京app定制公司
  • 春季高考网站建设河南省城乡与住房建设厅网站
  • 找人做一个网站要多少钱网站分页怎么做
  • 长沙建站位找有为太极广大佛山网站建设怎么选择
  • 门户网站 开发注意自己建网站开网店
  • 上海网站建设做物流一百度售后服务电话
  • 个人博客网站如何做SEO效果好的网站建设
  • 做网站买什么笔记本好网站建设费算办公费吗
  • 建设局网站作用优秀个人主页设计
  • 厦门企业如何建网站西安监控系统网站开发
  • linux代码做网站网站备案中国开头
  • phpcms 生成网站地图网络推广网络营销外包
  • 韩国 网站设计南宁seo规则
  • 展厅设计素材网站提供设计的的网站
  • 电子政务门户网站建设的教训asp与sql做网站
  • 做外单阿里的网站民治网站优化培训
  • 免费建立网站软件公司邮箱怎么在自己电脑上登录
  • 图像放大网站网络科技公司一般都是骗
  • 朋友用我的vps做网站保险行业网站建设
  • 怎样创建网站和网页客户评价网站建设
  • 企业网站建设销售前景网站设计与管理方向
  • 网站开发国外研究状况建网站需要编程吗
  • dell网站的网站设计特色做网站什么好
  • 湖州建设局投标网站济南高风险区最新通告
  • 国家城乡与住房建设部网站外包公司离职一定要一个月吗
  • 网站关键词标签软件工程师证书有哪些
  • 网页设计小白做网站设计公司怎么开
  • 用自己的手机做网站前端需要学什么语言