表格内容(本博客演示的表格,这里其实可以更换任意表格,动态展示的)

安装插件xlsx
复制
组件的所有代码(附解释)
| <script setup> |
| import { ref } from "vue"; |
| import * as XLSX from "xlsx"; |
| |
| function readFile(file) { |
| return new Promise((resolve) => { |
| let reader = new FileReader(); |
| reader.readAsBinaryString(file); |
| reader.onload = (ev) => { |
| resolve(ev.target.result); |
| }; |
| }); |
| } |
| |
| const title = ref([]); |
| const tableData = ref([]); |
| |
| |
| const handle = async (ev) => { |
| |
| title.value = []; |
| tableData.value = []; |
| let file = ev.raw; |
| |
| console.log("file", file); |
| |
| let data = await readFile(file); |
| |
| let workbook = XLSX.read(data, { type: "binary" }); |
| |
| console.log("workbook", workbook); |
| |
| let worksheet = workbook.Sheets[workbook.SheetNames[0]]; |
| |
| console.log("worksheet", worksheet); |
| |
| data = XLSX.utils.sheet_to_json(worksheet); |
| |
| |
| console.log("data", data); |
| |
| |
| for (const key in data[0]) { |
| title.value.push(key); |
| } |
| |
| console.log("title", title.value); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| data.forEach((item, index) => { |
| let obj = {}; |
| title.value.forEach((item2, index2) => { |
| console.log("item", item[item2]); |
| obj["in" + index2] = item[item2]; |
| }); |
| tableData.value.push(obj); |
| }); |
| |
| console.log("tableData.value", tableData.value); |
| }; |
| </script> |
| |
| <template> |
| <div class=""> |
| <el-upload |
| action |
| accept=".xlsx,.xls" |
| :show-file-list="false" |
| :auto-upload="false" |
| :on-change="handle" |
| > |
| <template #trigger> |
| <el-button type="primary">选择文件</el-button> |
| </template> |
| <el-button |
| class="ml-3" |
| type="success" |
| @click="submitUpload" |
| > |
| 提交到服务器(暂时无) |
| </el-button> |
| <template #tip> |
| <div class="el-upload__tip text-red"> |
| <el-table |
| :data="tableData" |
| style="width: 100%" |
| > |
| |
| <el-table-column |
| v-for="(item, index) in title" |
| :key="index" |
| :prop="'in' + index" |
| :label="item" |
| width="180" |
| /> |
| </el-table> |
| </div> |
| </template> |
| </el-upload> |
| </div> |
| </template> |
| |
| <style scoped lang="scss"></style> |
复制
效果图

上传表格后的效果图

更换下表格再展示


js上传表格(所有代码,不附讲解)
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta |
| http-equiv="X-UA-Compatible" |
| content="IE=edge" |
| /> |
| <meta |
| name="viewport" |
| content="width=device-width, initial-scale=1.0" |
| /> |
| <title>Document</title> |
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.full.min.js"></script> |
| </head> |
| <body> |
| |
| <input |
| type="file" |
| id="file-input" |
| onchange="loadExcelFile()" |
| /> |
| |
| |
| <div id="table-container"></div> |
| |
| <script> |
| function loadExcelFile() { |
| |
| const file = document.getElementById("file-input").files[0]; |
| |
| |
| const reader = new FileReader(); |
| reader.onload = function (e) { |
| |
| const data = new Uint8Array(e.target.result); |
| const workbook = XLSX.read(data, { type: "array" }); |
| const sheet = workbook.Sheets[workbook.SheetNames[0]]; |
| |
| |
| const htmlTable = XLSX.utils.sheet_to_html(sheet); |
| |
| console.log("htmlTable", htmlTable); |
| document.getElementById("table-container").innerHTML = htmlTable; |
| |
| |
| |
| const parser = new DOMParser(); |
| const doc = parser.parseFromString(htmlTable, "text/html"); |
| |
| const rows = doc.querySelectorAll("tr"); |
| |
| rows.forEach((row) => { |
| const cells = row.querySelectorAll("td"); |
| const values = Array.from(cells).map((cell) => |
| cell.textContent.trim() |
| ); |
| |
| console.log(values); |
| }); |
| }; |
| reader.readAsArrayBuffer(file); |
| } |
| </script> |
| </body> |
| </html> |
复制
js表格的效果图



再加一个带有导出表格的操作(这个是汇总的,既有导入表格又有导出表格)
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title>Document</title> |
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.full.min.js"></script> |
| </head> |
| |
| <body> |
| |
| <input type="file" id="file-input" onchange="loadExcelFile()" /> |
| |
| <button onclick="exportTable()">Export Table</button> |
| |
| |
| <div id="table-container"></div> |
| |
| <script> |
| let tableData = [] |
| |
| function loadExcelFile () { |
| |
| const file = document.getElementById("file-input").files[0] |
| |
| |
| const reader = new FileReader() |
| reader.onload = function (e) { |
| |
| const data = new Uint8Array(e.target.result) |
| const workbook = XLSX.read(data, { type: "array" }) |
| const sheet = workbook.Sheets[workbook.SheetNames[0]] |
| |
| |
| const htmlTable = XLSX.utils.sheet_to_html(sheet) |
| document.getElementById("table-container").innerHTML = htmlTable |
| |
| |
| const parser = new DOMParser() |
| const doc = parser.parseFromString(htmlTable, "text/html") |
| const rows = doc.querySelectorAll("tr") |
| |
| |
| tableData = [] |
| |
| |
| rows.forEach((row) => { |
| const cells = row.querySelectorAll("td") |
| const values = Array.from(cells).map((cell) => |
| cell.textContent.trim() |
| ) |
| tableData.push(values) |
| }) |
| } |
| reader.readAsArrayBuffer(file) |
| } |
| |
| function exportTable () { |
| if (tableData.length === 0) { |
| console.log("No table data available.") |
| return |
| } |
| |
| const worksheet = XLSX.utils.aoa_to_sheet(tableData) |
| const workbook = XLSX.utils.book_new() |
| XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1") |
| const excelBuffer = XLSX.write(workbook, { |
| bookType: "xlsx", |
| type: "array", |
| }) |
| |
| saveAsExcelFile(excelBuffer, "table_data") |
| } |
| |
| function saveAsExcelFile (buffer, fileName) { |
| const data = new Blob([buffer], { type: "application/octet-stream" }) |
| const url = window.URL.createObjectURL(data) |
| const link = document.createElement("a") |
| link.href = url |
| link.setAttribute("download", fileName + ".xlsx") |
| document.body.appendChild(link) |
| link.click() |
| document.body.removeChild(link) |
| } |
| </script> |
| </body> |
| |
| </html> |
复制