With the development of the Internet and the progress of society, the amount of data in various industries is increasing, and the processing of data is becoming more and more important. Among them, the Excel table is an important data processing tool. In front-end and back-end projects, it is becoming more and more common to implement the import and export functions of Excel tables. This article will introduce how to use Vue to import Excel table data.
1. Prerequisite knowledge
Before starting to introduce the implementation of Vue to import Excel table data, let's briefly describe the relevant pre-knowledge.
1.1 Excel table
Excel is a spreadsheet software developed by Microsoft, which is widely used in various industries. Excel tables can help users quickly process data and perform visual data analysis. Excel files can exist in .xlsx or .xls format, and use .xlsx format more.
1.2 Front-end framework Vue
Vue is a data-driven progressive JavaScript framework. It is easy to learn and use, with an elegant API and concise template syntax. The Vue framework can help us build web applications more efficiently.
1.3 Front-end UI framework ElementUI
ElementUI is a desktop component library based on Vue.js 2.0. It provides a wealth of basic components and business components, which can quickly help developers build high-quality Web applications.
2. Implementation steps
After understanding the pre-knowledge, let's start to introduce how to use Vue to import Excel table data.
2.1 Import xlsx file
In order to realize the import of Excel table data, you first need to import the Excel table file to the front end. For this, we can use xlsx
this JavaScript library to achieve. npm
Install the library using xlsx
:
npm install xlsx --save
In the Vue component, use the following code to upload and read files:
import XLSX from 'xlsx' methods: { handleChange(file) { const reader = new FileReader() reader.readAsBinaryString(file.raw) reader.onload = () => { const workbook = XLSX.read(reader.result, { type: 'binary' }) // ... } } }
In the above code, readAsBinaryString
the uploaded Excel file is converted into a binary string by calling the method, and the XLSX.read
string is parsed into a JavaScript object using the method.
2.2 Parsing Excel table data
After getting the JavaScript object, it can then be parsed and the data in it can be displayed. The specific steps are as follows:
const sheet = workbook.Sheets[workbook.SheetNames[0]] const data = XLSX.utils.sheet_to_json(sheet, { header: 1 })
In the above code, first workbook.SheetNames
obtain the name of the first Sheet of the Excel table, and use workbook.Sheets
attributes to obtain the data of the sheet. Next, XLSX.utils.sheet_to_json
a method is called to parse the form's data into a JavaScript array.
2.3 Render data to the table
The last step is to render the parsed data to the front-end page. We can use the Table component provided by ElementUI to achieve.
<template> <el-table :data="tableData"> <el-table-column label="编号" prop="id"></el-table-column> <el-table-column label="姓名" prop="name"></el-table-column> <!-- ... --> </el-table> </template> <script> export default { data() { return { tableData: [] } }, methods: { handleChange(file) { // 读取Excel文件 const reader = new FileReader() reader.readAsBinaryString(file.raw) reader.onload = () => { const workbook = XLSX.read(reader.result, { type: 'binary' }) // 解析Excel表格数据 const sheet = workbook.Sheets[workbook.SheetNames[0]] const data = XLSX.utils.sheet_to_json(sheet, { header: 1 }) this.tableData = data.slice(1).map((item) => { return { id: item[0], name: item[1], // ... } }) } } } } </script>
In the above code, use slice
the method to remove the first element of the array (that is, the header row of the table), and then use map
the method to parse each row of data into an object, and finally assign these objects to tableData
attributes.
3. Summary
This article introduces how to use Vue to import Excel table data. Through the above steps, we can upload and parse Excel table files on the front end, and use the Table component to display the parsed data on the front end page. When we need to add information in batches, it can be easily achieved.