最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3純前端表格數(shù)據(jù)的導(dǎo)出與導(dǎo)入實(shí)現(xiàn)方式

 更新時(shí)間:2025年01月16日 08:37:14   作者:IT-fly  
這篇文章主要介紹了如何在純前端環(huán)境下使用xlsx-js-style庫(kù)進(jìn)行Excel表格文件的下載,并自定義樣式,還提到了使用xlsx庫(kù)進(jìn)行Excel表格數(shù)據(jù)的導(dǎo)入,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

項(xiàng)目場(chǎng)景:

我期望達(dá)成在純前端環(huán)境下進(jìn)行 excel 表格文件的下載操作,與此同時(shí),對(duì)所下載的表格文件的樣式加以調(diào)整與優(yōu)化,使其呈現(xiàn)出更符合需求的外觀和格式布局,從而滿足特定的展示與使用要求。

表格數(shù)據(jù)導(dǎo)出

1、安裝 xlsx-js-style

提示:這里描述項(xiàng)目中遇到的問題:

先安裝庫(kù) xlsx-js-style

npm install xlsx-js-style

2、自定義組件

自定義組件,這里附全部代碼,設(shè)置單元格格式代碼也在其中

<script lang="ts" setup>
import { computed, nextTick, ref, watch } from "vue"

import * as XLSX from "xlsx-js-style"

interface Props {
  modelValue: boolean
  downloadList: any
  columnList: any
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  downloadList: [],
  columnList: []
})

const downloadList = computed(() => props.downloadList)

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): boolean
}>()

const columnList = computed(() => props.columnList)

//#region 導(dǎo)出
const tableRef = ref<any>(null)
// 導(dǎo)出為 Excel
const exportToExcel = () => {
  // 獲取 el-table 的引用
  tableRef.value = document.querySelector("#download-table")

  // 將 el-table 數(shù)據(jù)轉(zhuǎn)換為二維數(shù)組
  const dataArray = []
  const headers: any = []

  tableRef.value.querySelectorAll(".el-table__header-wrapper th").forEach((th: any) => {
    headers.push(th.textContent.trim())
  })
  dataArray.push(headers)

  const rowsToExport = tableRef.value.querySelectorAll(".el-table__body-wrapper tbody tr")
  console.log(rowsToExport, "rowsToExport")

  rowsToExport.forEach((row: any) => {
    const rowData: any = []
    row.querySelectorAll("td").forEach((cell: any) => {
      rowData.push(cell.textContent.trim())
    })
    dataArray.push(rowData)
  })

  // 創(chuàng)建一個(gè)新的工作簿
  const workbook = XLSX.utils.book_new()

  // 創(chuàng)建一個(gè)新的工作表
  const worksheet = XLSX.utils.aoa_to_sheet(dataArray)

  console.log(dataArray, "dataArray")

  // 設(shè)置列寬
  const columnWidth = 20 // 列寬為20個(gè)字符
  const numColumns = dataArray[0].length // 獲取列數(shù)
  worksheet["!cols"] = Array(numColumns).fill({ wch: columnWidth })

  // // 設(shè)置列高
  const rowHeight = 105 // 行高為110像素
  const numRows = dataArray.length // 獲取行數(shù)
  worksheet["!rows"] = Array(numRows).fill({ hpx: rowHeight })
  worksheet["!rows"][0] = { hpx: 15 } // 單獨(dú)設(shè)置第一行的行高
  // 設(shè)置單元格樣式
  const cellStyle = {
    font: {
      name: "微軟黑體", // 字體名稱
      sz: 13, // 字體大小
      color: { rgb: "000000" }, // 字體顏色
      bold: false // 字體不加粗
    },
    border: {
      top: { style: "thin", color: { rgb: "000000" } },
      bottom: { style: "thin", color: { rgb: "000000" } },
      left: { style: "thin", color: { rgb: "000000" } },
      right: { style: "thin", color: { rgb: "000000" } }
    },
    alignment: {
      horizontal: "center",
      vertical: "center",
      wrapText: true // 設(shè)置文字自動(dòng)換行
    }
  }

  // 遍歷數(shù)據(jù)數(shù)組,為每個(gè)單元格應(yīng)用樣式
  dataArray.forEach((row, rowIndex) => {
    row.forEach((cell: any, columnIndex: any) => {
      const cellAddress = XLSX.utils.encode_cell({ c: columnIndex, r: rowIndex })
      worksheet[cellAddress].s = cellStyle
    })
  })

  // 表頭顏色加粗
  if (dataArray[0]) {
    dataArray[0].forEach((cell: any, columnIndex: any) => {
      const cellAddress = XLSX.utils.encode_cell({ c: columnIndex, r: 0 })
      worksheet[cellAddress].s = { ...cellStyle, font: { ...cellStyle.font, bold: true } } // 確保表頭字體加粗
    })
  }

  // 將工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1")

  // 將工作簿保存為 Excel 文件

  const timestamp = new Date().getTime()
  const fileName = `寶貝統(tǒng)計(jì)_${timestamp}.xlsx`
  XLSX.writeFile(workbook, fileName)
  emit("update:modelValue", false)
}
//#endregion

watch(
  () => props.modelValue,
  (value: any) => {
    if (value) {
      console.log(downloadList.value)
      nextTick(() => {
        exportToExcel()
      })
    }
  }
)
</script>
<template>
  <el-table style="display: none" :data="downloadList" id="download-table">
    <el-table-column
      v-for="(item, index) in columnList"
      :key="index"
      :prop="item.prop"
      :label="item.label"
      align="center"
    />
  </el-table>
</template>
<style lang="scss" scoped></style>

3、使用

使用是時(shí)候是相當(dāng)于在頁(yè)面上設(shè)置個(gè)不可間=見的表格進(jìn)行下載 將需要下載的數(shù)據(jù)傳入即可,可以進(jìn)行改進(jìn),表單項(xiàng)也傳入,這樣就可以隨意下載任何數(shù)據(jù)了

import DownloadExcel from "@/components/DownloadExcel/index.vue"
const columnList = [
  {
    prop: "index",
    label: "序號(hào)"
  },
  {
    prop: "username",
    label: "用戶名"
  },
  {
    prop: "roles",
    label: "角色"
  },
  {
    prop: "phone",
    label: "手機(jī)號(hào)"
  },
  {
    prop: "email",
    label: "郵箱"
  },
  {
    prop: "status",
    label: "狀態(tài)"
  },
  {
    prop: "createTime",
    label: "創(chuàng)建時(shí)間"
  }
]
const downloadList = ref<any[]>([])
const isDownload = ref<boolean>(false)
const exportToExcel = () => {
  if (multipleSelection.value.length == 0) {
    ElMessage.error("請(qǐng)選擇要下載的寶貝")
    return
  }
  downloadList.value = multipleSelection.value.map((item: any, index: number) => {
    return {
      index: index, // 序號(hào)
      username: item.username, // 用戶名
      roles: item.roles, // 角色
      phone: item.phone, // 手機(jī)號(hào)
      email: item.email, // 郵箱
      status: item.status ? "啟用" : "禁用", // 狀態(tài)
      createTime: item.createTime // 創(chuàng)建時(shí)間
    }
  })
  isDownload.value = true
}
<download-excel v-model="isDownload" :downloadList="downloadList" :columnList="columnList" />
<div>
  <el-tooltip content="下載">
    <el-button type="primary" :icon="Download" circle @click="exportToExcel" />
  </el-tooltip>
</div>

表格數(shù)據(jù)導(dǎo)入

1、安裝 xlsx

npm install xlsx --save

2、引入

import * as XLSX from "xlsx"

3、使用

<el-upload
  class="upload-demo"
  ref="upload"
  action=""
  :auto-upload="false"
  accept=""
  :on-change="analysisExcel"
  multiple
  :show-file-list="false"
>
  <el-button type="primary" :icon="Link">導(dǎo)入文件列表</el-button>
</el-upload>
import * as XLSX from "xlsx"
const loading = ref<boolean>(false)
const tableData = ref<any[]>([])
const analysisExcel = (file: any) => {
  loading.value = true
  console.log(file)
  // 只能上傳一個(gè)Excel,重復(fù)上傳會(huì)覆蓋之前的
  file = file.raw
  const reader = new FileReader()
  reader.readAsArrayBuffer(file)
  reader.onload = function () {
    const buffer: any = reader.result

    const bytes = new Uint8Array(buffer)
    const length = bytes.byteLength
    let binary = ""
    for (let i = 0; i < length; i++) {
      binary += String.fromCharCode(bytes[i])
    }
    const wb = XLSX.read(binary, {
      type: "binary"
    })
    const outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
    console.log(outdata)
    let da = [...outdata]
    // 這里是把表格里面的名稱改成表格里面的字段
    da = da.map((item: any) => {
      const newItem = {
        index: Number(item["序號(hào)"]), // 序號(hào)
        username: item["用戶名"], // 用戶名
        roles: item["角色"], // 角色
        phone: item["手機(jī)號(hào)"], // 手機(jī)號(hào)
        email: item["郵箱"], // 郵箱
        status: item["狀態(tài)"], // 狀態(tài)
        createTime: item["創(chuàng)建時(shí)間"] // 創(chuàng)建時(shí)間
      }
      return newItem
    })
    console.log(da)

    tableData.value = da
    loading.value = false
  }
}

總結(jié) 

到此這篇關(guān)于vue3純前端表格數(shù)據(jù)的導(dǎo)出與導(dǎo)入實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue3表格數(shù)據(jù)導(dǎo)出與導(dǎo)入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue單頁(yè)面如何通過prerender-spa-plugin插件進(jìn)行SEO優(yōu)化

    vue單頁(yè)面如何通過prerender-spa-plugin插件進(jìn)行SEO優(yōu)化

    這篇文章主要介紹了vue單頁(yè)面如何通過prerender-spa-plugin插件進(jìn)行SEO優(yōu)化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue跨窗口通信之新窗口調(diào)用父窗口方法實(shí)例

    vue跨窗口通信之新窗口調(diào)用父窗口方法實(shí)例

    由于開發(fā)需要,我需要在登錄成功請(qǐng)求成功后,調(diào)用父窗口的一個(gè)點(diǎn)擊事件方法,這篇文章主要給大家介紹了關(guān)于vue跨窗口通信之新窗口調(diào)用父窗口的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目的實(shí)例代碼

    vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目的實(shí)例代碼

    這篇文章主要介紹了vue-element-admin+flask實(shí)現(xiàn)數(shù)據(jù)查詢項(xiàng)目,填寫數(shù)據(jù)庫(kù)連接信息和查詢語(yǔ)句,即可展示查詢到的數(shù)據(jù),需要的朋友可以參考下
    2022-11-11
  • Vue.js系列之項(xiàng)目搭建(1)

    Vue.js系列之項(xiàng)目搭建(1)

    今天要講講Vue2.0了。最近將公司App3.0用vue2.0構(gòu)建了一個(gè)web版,因?yàn)槭堑谝淮问褂胿ue,而且一開始使用的時(shí)候2.0出來一個(gè)月不到,很多坑都是自己去踩的,現(xiàn)在項(xiàng)目要上線了,所以記錄一些過程
    2017-01-01
  • npm run serve運(yùn)行vue項(xiàng)目時(shí)報(bào)錯(cuò):Error: error:0308010C:digital envelope routines::unsupported的解決方法

    npm run serve運(yùn)行vue項(xiàng)目時(shí)報(bào)錯(cuò):Error: error:0308010C

    這篇文章主要介紹了npm run serve運(yùn)行vue項(xiàng)目時(shí),出現(xiàn)報(bào)錯(cuò):Error: error:0308010C:digital envelope routines::unsupported的解決方法,文中有詳細(xì)的解決方法,需要的朋友可以參考下
    2024-04-04
  • avue實(shí)現(xiàn)自定義搜索欄及清空搜索事件的實(shí)踐

    avue實(shí)現(xiàn)自定義搜索欄及清空搜索事件的實(shí)踐

    本文主要介紹了avue實(shí)現(xiàn)自定義搜索欄及清空搜索事件的實(shí)踐,主要包括對(duì)搜索欄進(jìn)行自定義,并通過按鈕實(shí)現(xiàn)折疊搜索欄效果,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12
  • vue之computed的緩存特性

    vue之computed的緩存特性

    這篇文章主要介紹了vue之computed的緩存特性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vue 清空input標(biāo)簽 中file的值操作

    vue 清空input標(biāo)簽 中file的值操作

    這篇文章主要介紹了vue 清空input標(biāo)簽 中file的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue2.0安裝style/css loader的方法

    vue2.0安裝style/css loader的方法

    下面小編就為大家分享一篇vue2.0安裝style/css loader的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 淺談Vue+Ant Design form表單的一些坑

    淺談Vue+Ant Design form表單的一些坑

    本文主要介紹了淺談Vue+Ant Design form表單的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評(píng)論

开阳县| 顺义区| 九台市| 枣强县| 东兴市| 白玉县| 营口市| 靖宇县| 中超| 韩城市| 大新县| 安阳县| 宁津县| 崇仁县| 鄂伦春自治旗| 铁岭市| 绵竹市| 颍上县| 武鸣县| 会昌县| 东阳市| 当阳市| 沭阳县| 贵港市| 株洲县| 屏东县| 清徐县| 财经| 敖汉旗| 吴旗县| 广宁县| 中西区| 江华| 秦皇岛市| 普陀区| 锡林郭勒盟| 蒙山县| 湖口县| 花莲县| 汶川县| 余江县|