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

wap手机网站开发软件最火的推广平台

wap手机网站开发软件,最火的推广平台,房地产交易中心官网,广州推广seo导出:解析excel 二进制文件 https://blog.csdn.net/weixin_43746297/article/details/146204040 导入:解析excel 二进制文件 解析: 注意:使用前需通过npm install xlsx安装依赖库 动态表头生成 使用 sheet_to_json 的 header…

导出:解析excel 二进制文件

https://blog.csdn.net/weixin_43746297/article/details/146204040

导入:解析excel 二进制文件

解析:

注意:使用前需通过npm install xlsx安装依赖库

动态表头生成

使用 sheet_to_json 的 header:1 参数提取首行作为表头

Excel工作表转换为JSON格式:

const jsonData = XLSX.utils.sheet_to_json(firstSheet, {header: 1,raw: false, //保留公式defval: ""
})
参数配置如下:
  • header:  指定输出结果为二维数组格式,每行数据以数组形式呈现。若不设置该参数或设为其他值,则默认输出键值对格式的对象数组。

  • raw: false 保留单元格原始公式而非计算结果,适用于需要获取Excel公式的场景。若设为true则输出公式计算结果。

  • defval: "" 为空单元格设置默认值为空字符串,避免解析时跳过空值导致字段缺失。未设置时默认会忽略空单元格。

jsonData结构取决于header参数:
  • header:1时:结果为Array<Array<any>>,每行数据为子数组
  • 默认情况:结果为Array<Object>,首行作为键名
典型应用场景包括:
  • 前端导入带公式的Excel表格
  • 需要严格保留空单元格位置的表格处理
  • 复杂表头数据的结构化转换

使用xlsx库读取Excel数据

const workbook = XLSX.read(data, {type: 'array',cellFormula: true,cellHTML: false});
主要功能如下:
  • type: 'array'参数表示输入数据是Uint8Array类型的二进制数组
  • cellFormula: true会保留Excel单元格中的公式信息,而不是只获取计算结果
  • cellHTML: false表示不需要将单元格内容转换为HTML格式
workbook对象包含属性:
  • SheetNames属性(工作表名称列表)
  • Sheets对象(包含各个工作表数据)
典型使用场景包括:
  • 前端导入带公式的Excel文件
  • 需要保留原始公式而非计算结果的场景

从接口中获取

1.封装接口方法 获取excel字节流:

封装示例

封装方法,调用方法和传参形式依照自己的,这里只是示例

注意:responseType: 'arraybuffer'   必填***

 调用方法
  exportHandover({'传参':'传参'}).then((res) => {// 获取到二进制流  res}).catch((err) => {//弹出错误信息});
  •  这里是解析二进制流之后的数据格式 可自定义,可查看 下方完整代码
        arr.push({sheetName,columns: headers.map((h, i) => ({prop: `col${i}`,label: h})),list: [{columns: headers.map((h, i) => ({prop: `col${i}`,label: h})),data: dataRows.map(row => {return headers.reduce((obj, header, idx) => {obj[`col${idx}`] = row[idx] || ""return obj}, {})})}]})
完整代码 :

此段代码适用于 excel 单文件/多文件

<template><div class="layout-padding w100"><div class="layout-padding-auto layout-padding-view"><el-tabs style="height: calc(100% - 50px)" v-model="activeFile" type="card"><el-tab-panev-for="file in fileData":key="file.sheetName":label="file.sheetName":name="file.sheetName"class="table-container"><el-table:data="file.list"border:header-cell-style="{ borderColor: '#C0C0C0',textAlign:'center' }":cell-style="{ borderColor: '#C0C0C0',textAlign:'center '}"><el-table-columnv-for="col in file.columns":key="col.prop":prop="col.prop":label="col.label"/></el-table></el-tab-pane></el-tabs></div></div>
</template><script setup lang="ts" name="team">
import {useI18n} from 'vue-i18n';
import {reactive, ref, onMounted} from 'vue';
import {viewDeta} from '/@/stores/viewDeta';
import {exportHandover} from "/@/api/shiftHandover";
import {downBlobFile} from '/@/utils/other';
import {ElLoading} from 'element-plus';
import * as XLSX from 'xlsx';
import {useMessage} from "/@/hooks/message";const stores = viewDeta();
// 引入组件
const {t} = useI18n();
// 定义变量内容
const editFormDialog = ref();
const fileData = ref()
const activeFile = ref()
const queryForm = reactive({yearMonth: new Date().toISOString().slice(0, 7),deptId: '',roleId: '',
})
const getDeptData = () => {if (stores.$state.rolesName != undefined) {const loading = ElLoading.service({lock: true,text: '数据加载中...',background: 'rgba(5,5,5,0.6)',})exportHandover({参数}).then((res) => {const data = new Uint8Array(res)loading.close()const workbook = XLSX.read(data, {type: 'array',cellFormula: true,cellHTML: false});let arr = []workbook.SheetNames.forEach((sheetName) => {const firstSheet = workbook.Sheets[sheetName]const jsonData = XLSX.utils.sheet_to_json(firstSheet, {header: 1,raw: false,defval: ""})const headers = jsonData[0] || []const dataRows = jsonData.slice(1)arr.push({sheetName,columns: headers.map((h, i) => ({prop: `col${i}`,label: h})),list: [{columns: headers.map((h, i) => ({prop: `col${i}`,label: h})),data: dataRows.map(row => {return headers.reduce((obj, header, idx) => {obj[`col${idx}`] = row[idx] || ""return obj}, {})})}]})})arr.map((item) => {item.list = item.list.flatMap(r => r.data)})fileData.value = arractiveFile.value = fileData.value[0].sheetName}).catch((err) => {useMessage().error(err.msg);loading.close()});}
};onMounted(() => {
});
</script>
<style scoped lang="scss">
.table-container {display: flex;flex-direction: column;height: 100%; /* 或父容器高度 */
}.table-container > .el-table {flex: 1;min-height: 0; /* 关键属性 */
}
</style>
结果:

2.ajax方法直接获取

接口路径:/api/excel

使用axios时需设置 responseType: 'arraybuffer' 'blob'接收二进制流

 const res = await axios.get('/api/excel', {responseType: 'arraybuffer'
});
完整代码:

此代码适用于单文件,如需多文件 参考上方封装接口方法

<template><div><button @click="loadExcelData">加载Excel数据</button><table v-if="tableData.length"><thead><tr><th v-for="col in columns" :key="col.key">{{ col.title }}</th></tr></thead><tbody><tr v-for="(row, rowIndex) in tableData" :key="rowIndex"><td v-for="col in columns" :key="col.key">{{ row[col.key] }}</td></tr></tbody></table></div>
</template><script setup>
import { ref } from 'vue';
import axios from 'axios';
import * as XLSX from 'xlsx';// 表格数据和列配置
const tableData = ref([]);
const columns = ref([]);// 从接口加载Excel数据
const loadExcelData = async () => {try {// 1. 请求二进制数据(关键:responseType: 'arraybuffer')const response = await axios.get('https://your-api.com/excel', {responseType: 'arraybuffer'});// 2. 解析Excelconst data = new Uint8Array(response.data);const workbook = XLSX.read(data, { type: 'array' });const firstSheet = workbook.Sheets[workbook.SheetNames[0]];// 3. 提取表头(假设第一行为表头)const headerRow = XLSX.utils.sheet_to_json(firstSheet, { header: 1 })[0];columns.value = headerRow.map(key => ({key,title: key}));// 4. 提取表格数据tableData.value = XLSX.utils.sheet_to_json(firstSheet);} catch (error) {console.error('加载失败:', error);}
};
</script>

 

上传文件获取

此方法适用于excel中单文件,如excel中多文件 请参考上方 封装接口方法


<template><div><el-uploadmultiple:auto-upload="false":on-change="handleFileChange"accept=".xlsx,.xls"><el-button type="primary">选择多个Excel文件</el-button></el-upload><el-table v-for="(table, index) in tables" :key="index":data="table.data"borderstyle="margin-top:20px"><el-table-columnv-for="col in table.columns":key="col.prop":prop="col.prop":label="col.label":formatter="col.formatter"/></el-table></div>
</template><script setup>
import { ref } from 'vue'
import * as XLSX from 'xlsx'const tables = ref([])const handleFileChange = async (files) => {tables.value = []for (const file of files) {const buffer = await file.raw.arrayBuffer()const workbook = XLSX.read(buffer, { type: 'array',cellFormula: true // 保留公式})workbook.SheetNames.forEach(sheetName => {const worksheet = workbook.Sheets[sheetName]const jsonData = XLSX.utils.sheet_to_json(worksheet, {header: 1,raw: false,defval: ""})if(jsonData.length > 0) {const columns = jsonData[0].map((header, idx) => ({prop: `col${idx}`,label: header || `列${idx+1}`,formatter: (row) => {const cellRef = XLSX.utils.encode_cell({r: row.__rowNum__, c: idx})return worksheet[cellRef]?.f ? XLSX.utils.sheet_to_json(worksheet, {header:1})[row.__rowNum__][idx] :row[`col${idx}`]}}))const data = jsonData.slice(1).map((row, rowIndex) => {const rowObj = { __rowNum__: rowIndex + 1 }row.forEach((cell, colIndex) => {rowObj[`col${colIndex}`] = cell})return rowObj})tables.value.push({name: `${file.name}-${sheetName}`,columns,data})}})}
}
</script>

http://www.mmbaike.com/news/108042.html

相关文章:

  • 重庆景点图片高清图片泉州关键词优化软件
  • 电子商务的网站建设要求步骤网络销售公司经营范围
  • 网站建设维护公司网络优化工程师工作内容
  • 教人做素食的网站app推广引流
  • 网站宣传的方法主要有seo翻译
  • 怎么使用网站服务器企业qq一年多少费用
  • dede 网站源码佛山网站建设公司哪家好
  • 成都哪家公司做网站最好信息流优化师简历怎么写
  • 开发一个网站做爬虫整合营销传播工具有哪些
  • 如何制作网站板块百度竞价排名收费
  • 张家港网站推广优化近期10大新闻事件
  • 制作自己的网站学校汽车营销策划方案ppt
  • 橙象品牌设计宁波seo外包引流推广
  • 学校网站首页站长之家 站长工具
  • 独立站如何推广免费手游推广平台
  • 网站访问流量怎么赚钱软文的概念
  • 域名备案和网站备案是一回事吗百度搜索什么关键词排名
  • 做一个购物网站需要多久百度seo快速
  • 网站子域名查询给企业做网站的公司
  • 简单的企业网站域名购买
  • 网站推广软文甄选天天软文郑州seo线上推广系统
  • 以下哪一项不属于seo对网站推广的作用搜索引擎有哪些种类
  • 河南省建设厅执业资格注册中心网站网推广公司
  • 广东网站建设加工seo优化一般包括哪些内容()
  • 那个b2b网站可以做外贸百度seo高级优化
  • 河北廊坊做网站网址大全是ie浏览器吗
  • 南宁网络营销策划推广公司网络优化的基本方法
  • 网站自动售卡怎么做广西seo经理
  • 寒亭区住房和城乡建设局网站100个经典创意营销方案
  • 怎么在建筑网站做翻译兼职搜狗收录查询