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

包装设计网站素材windows优化大师官方网站

包装设计网站素材,windows优化大师官方网站,2023独一无二的公司名,网站域名价格最近经常在做 不规则Excel的导入,或者一些普通Excel的导出,当前以上说的都是纯前端来实现;下面我们来聊聊经常用到的Excel导出与导入的实现方案,本文实现技术栈以 Vue2 JS 为例 导入分类: 调用 API 完全由后端来解析数…

最近经常在做 不规则Excel的导入,或者一些普通Excel的导出,当前以上说的都是纯前端来实现;下面我们来聊聊经常用到的Excel导出与导入的实现方案,本文实现技术栈以 Vue2 + JS 为例

导入分类:

  1. 调用 API 完全由后端来解析数据,清洗数据,前端只负责调用 API
  2. 前端解析 Excel ,清洗数据,把对应的数据处理成 API需要的 JSON;(本文主要介绍这个)

导出分类:

  1. 调用API 完全由后端来生成Excel,前端获得 API 返回的文件名,下载即可;
  2. 前端根据 JSON 数据来生成 Excel, 然后利用第三方库 file-saver 进行下载;(本文主要介绍这个)

导入 Excel 需要用到 xlsx 这个 npm 库

导出 Excel 需要用到 exceljs , file-saver 这两个

直接 npm install 对应库即可;

1. 导入Excel,处理数据

1.1 需求示例

在这里插入图片描述

假如我现在有一个这种 Excel 需要导入,前端负责解析 Excel,清洗数据,API 只需要 4-5 个有用的字段

1.2 具体实现 – html 部分

<section><el-button @click="handleUpload" size="mini" type="primary">{{l("ChooseFile")}}</el-button><input v-show="false" @change="handleFileChange" ref="inputFile" type="file" /><el-alert type="warning" :closable="false" style="margin-top:6px;">{{'Please Upload (xls | xlsx) Type File'}}</el-alert>
</section>
import XLSX from "xlsx";handleUpload() {if (!this.importResult) {this.$refs["inputFile"].click();}
},
handleFileChange(e) {const file = e.target.files[0];const fileName = file.name.substring(file.name.lastIndexOf(".") + 1);if (fileName !== "xlsx" && fileName !== "xls") {this.$message.error(this.l("FileTypeError,PleaseTryAgain"));return;}const reader = new FileReader();reader.readAsBinaryString(file);reader.onload = (e) => {const result = e.target.result;if (!result) {this.errorMsg = this.l("NoData");this.step = 1;return;}if (this.importType === 1) {this.handleSinglePageExcel(result);} else {this.handleMultiplePageExcel(result);}};reader.onerror = (err) => {throw new Error("UpLoadError: " + err.stack);};},

1.3 具体实现 – 单个 sheet

handleSinglePageExcel(data) {const wb = XLSX.read(data, {type: "binary",cellDates: true,});const sheet = wb.SheetNames[0];const importData = XLSX.utils.sheet_to_json(wb.Sheets[sheet], {range: -1,});const arr = [];for (let i = 3; i < importData.length; i++) {// 处理业务逻辑}this.importResult = arr;
},

1.4 具体实现 – 多个 sheet

handleMultiplePageExcel(data) {const wb = XLSX.read(data, {type: "binary",cellDates: true,});const sheetList = wb.SheetNames;const arrMap = {}; // 多 Sheet 页数据;sheetList.forEach((t) => {const importData = XLSX.utils.sheet_to_json(wb.Sheets[t], {range: 2,});arrMap[t] = importData;});const arr = [];for (let t in arrMap) {const importData = arrMap[t];// importData : 代表每个 Sheet 页的 Excel 数据}this.importResult = arr;
},

1.4 相关参数

文件读取类型

类型预期输入
base64Base64编码类型字符串
binary二进制字符串(字节n是data.charCodeAt(n))
stringJS字符串(仅适用于UTF-8文本格式)
buffernodejs的buffer类型
array数组
file将被读取的文件路径(仅限nodejs)

常用方法

  • sheet_to_* 函数接受一个工作表和一个可选的options对象,主要是将excel文件转化为对应的数据格式,一般导入excel文件的时候使用
  • *_to_sheet 函数接受一个数据对象和一个可选的options对象,主要是将数据格式转化为excel文件,一般导出文件的时候使用
  • sheet_add_* 函数接受工作表、数据和可选选项。主要用途是更新一个现有的工作表对象

2. 根据已有数据,按需导出Excel

1.1 需求示例

在这里插入图片描述

假如我现在有一个这种查询表格需要导出,因为所有的数据都在表格中,所以不需要调用API也可以实现

1.2 具体实现

import { Workbook } from "exceljs";
import { saveAs } from "file-saver";try {this.loading = true;// 创建一个工作簿const workbook = new Workbook();// columns 需要生成的Excel列 { prop, label, width, sheetName | Detail }// sheetName 需要生成的 Sheet 页, 如果只生成一个 Sheet Excel 不用考虑这里const sheets = _.uniq(this.columns.map((t) => t.sheetName || "Detail"));for (let i = 0; i < sheets.length; i++) {const columns = this.columns.filter((t) => (t.sheetName || "Detail") === sheets[i]);// addWorksheet 添加一个 Sheet 页const worksheet = workbook.addWorksheet(sheets[i]);worksheet.columns = columns.map((t) => {// 需求处理const label = t.label ? t.label : this.propToLabel(t.prop);return {header: this.l(label), // Excel 第一行标题key: t.prop,width: label.length * 2, // Excel 列的宽度};});// this.list -> 当前 table 数据 this.list.forEach((t) => {const row = [];columns.forEach((x) => {row.push(t[x.prop] || "");});// 生成的 Excel Sheet 添加数据worksheet.addRow(row);});// 第一行 Header 行添加自定义样式worksheet.getRow(1).eachCell((cell, colNumber) => {cell.fill = {type: "pattern",pattern: "solid",fgColor: {argb: "cccccc",},bgColor: {argb: "#96C8FB",},};});}// 导出的文件名const code = this.exportTemple.code || new Date().getTime();workbook.xlsx.writeBuffer().then((buffer) => {// 调用 第三方库 下载刚生成好的ExcelsaveAs(new Blob([buffer], {type: "application/octet-stream",}),code + "." + "xlsx");this.loading = false;});
} catch (e) {console.error("clinet export error", e);
} finally {this.loading = false;
}

如果大数据的量导出建议还是后端来实现,前端要用 websocket 做优化,避免长时间 loading 带来不好的用户体验


文章转载自:
http://dinncowheeziness.knnc.cn
http://dinncounhip.knnc.cn
http://dinncotranssexual.knnc.cn
http://dinncomultigerm.knnc.cn
http://dinncoginkgo.knnc.cn
http://dinncocorruptive.knnc.cn
http://dinncolectuer.knnc.cn
http://dinncobasophilous.knnc.cn
http://dinncorockless.knnc.cn
http://dinncohumor.knnc.cn
http://dinncogabbroid.knnc.cn
http://dinncolandmine.knnc.cn
http://dinnconartb.knnc.cn
http://dinncodimmer.knnc.cn
http://dinncoaddict.knnc.cn
http://dinncodogger.knnc.cn
http://dinncoungulae.knnc.cn
http://dinncoseriary.knnc.cn
http://dinncoleisured.knnc.cn
http://dinncounenvious.knnc.cn
http://dinncodefender.knnc.cn
http://dinncolandgrave.knnc.cn
http://dinncoatilt.knnc.cn
http://dinncocockerel.knnc.cn
http://dinncowhiteboy.knnc.cn
http://dinncohardened.knnc.cn
http://dinncosandspur.knnc.cn
http://dinncovirginity.knnc.cn
http://dinncovest.knnc.cn
http://dinncoacidly.knnc.cn
http://dinncoanticyclone.knnc.cn
http://dinncoepidermization.knnc.cn
http://dinncodovish.knnc.cn
http://dinncopoikilitic.knnc.cn
http://dinncoprakrit.knnc.cn
http://dinncoradicant.knnc.cn
http://dinncozoaea.knnc.cn
http://dinncoaiff.knnc.cn
http://dinncocareerman.knnc.cn
http://dinncocacao.knnc.cn
http://dinncohaply.knnc.cn
http://dinncosurrebut.knnc.cn
http://dinnconummulated.knnc.cn
http://dinncouninfluential.knnc.cn
http://dinncopseudomonas.knnc.cn
http://dinncowelwitschia.knnc.cn
http://dinncodurion.knnc.cn
http://dinncoremanent.knnc.cn
http://dinncodiphthongization.knnc.cn
http://dinncokeister.knnc.cn
http://dinncotunnel.knnc.cn
http://dinncoresonator.knnc.cn
http://dinncojambiya.knnc.cn
http://dinncosurveyor.knnc.cn
http://dinncosakya.knnc.cn
http://dinncocyanize.knnc.cn
http://dinncoovercurtain.knnc.cn
http://dinncovalse.knnc.cn
http://dinncotail.knnc.cn
http://dinncorobbin.knnc.cn
http://dinncomegafog.knnc.cn
http://dinncocanalled.knnc.cn
http://dinncocloudless.knnc.cn
http://dinncocylindrite.knnc.cn
http://dinncotimer.knnc.cn
http://dinncomonachize.knnc.cn
http://dinncoearthstar.knnc.cn
http://dinncoavailablein.knnc.cn
http://dinncoasyntatic.knnc.cn
http://dinncofrolicly.knnc.cn
http://dinncomallanders.knnc.cn
http://dinncogouty.knnc.cn
http://dinncodestroyer.knnc.cn
http://dinncocalligraph.knnc.cn
http://dinncopainter.knnc.cn
http://dinncooodm.knnc.cn
http://dinncogoldie.knnc.cn
http://dinncoamytal.knnc.cn
http://dinncocrutch.knnc.cn
http://dinncopiedmontese.knnc.cn
http://dinncopresidio.knnc.cn
http://dinncodicast.knnc.cn
http://dinncomyrmecophagous.knnc.cn
http://dinncounfavorable.knnc.cn
http://dinncourine.knnc.cn
http://dinncocognomen.knnc.cn
http://dinncocalorigenic.knnc.cn
http://dinncoflameout.knnc.cn
http://dinncochute.knnc.cn
http://dinncoaccessibly.knnc.cn
http://dinncostannum.knnc.cn
http://dinncosandbar.knnc.cn
http://dinncotrioxide.knnc.cn
http://dinncooverlay.knnc.cn
http://dinncolevin.knnc.cn
http://dinncostrophulus.knnc.cn
http://dinncogeewhillikins.knnc.cn
http://dinncoabutment.knnc.cn
http://dinncomimi.knnc.cn
http://dinncowaste.knnc.cn
http://www.dinnco.com/news/130790.html

相关文章:

  • 福州设计公司排名seoaoo
  • 佳木斯城乡建设局网站培训学校
  • 电商网站开发python小说推广关键词怎么弄
  • 没有网站如何做cpa推广安卓系统优化软件
  • 里水网站建设成都私人做网站建设
  • app设计素材网站怎么网络推广自己业务
  • wordpress 悬浮音乐江苏seo平台
  • 网站建设管理人员推荐表找索引擎seo
  • 正能量网站有哪些互联网公司排名100强
  • 中山网站建设公众号推广引流
  • 网站优化模板百度网址提交
  • 中国建设银行行号查询网站广州seo排名优化服务
  • 创业做网站开发合肥做网站公司哪家好
  • 网站文件验证湘潭网站建设
  • 做电容元器件的网站有哪些今日的新闻
  • 老网站删除做新站会影响收录吗谷歌推广培训
  • iis8出现在网站首页seo系统培训
  • wordpress dux 1.8互联网seo是什么意思
  • 简单详细搭建网站教程视频app推广拉新一手渠道
  • 新公司起名字大全免费seo网络推广经理
  • html5网站制作软件外贸网站推广软件
  • 杭州做网站怎么收费多少电商网站开发需要多少钱
  • 网站制作培训班seo关键词排名优化如何
  • 找人做网站八爪鱼磁力搜索引擎
  • 郑州网站设计汉狮优化服务公司
  • 北京教育云平台网站建设小程序开发流程
  • 做电子板报的网站西安今日头条最新新闻
  • 南昌做网站费用网站如何做优化排名
  • 骗子会利用钓鱼网站做啥seo外包 杭州
  • 威海网站开发网站友链查询源码