1. Download the xlsx plug-in
npm i xlsx
2. Upload files through the upload component of the element-ui component
<el-upload class="upload-demo" action //Required parameters, upload address, we do not write the address here to customize upload accept=".xlsx,.xls" //File Type :auto-upload="false" //Whether to upload immediately after selecting the file :show-file-list="false" // Whether to display the uploaded file list :on-change="handleChange" //Hook when file status changes, it will be called when adding files, uploading successfully and uploading fails > <el-button type="primary">Select a file</el-button> </el-upload>
3. Convert the selected Excel file to binary
//Read the binary fileexport function readFile(file) { return new Promise((resolve, reject) => { // FileReader is mainly used to read file content into memory. Through a series of asynchronous interfaces, local files can be accessed in the main thread. let reader = new FileReader(); // Read the file content asynchronously bytes, and the result is a binary string of the file (file); = (ev) => { resolve(); }; }); }
4. Read binary data through () in the plug-in
//Select fileconst handleChange = async function (e) { const file = ; // Selected file content let data = await readFile(file); //Convert content to binary let workbook = (data, { type: "binary" }); //Read binary data through plug-in binary}
5. Convert the data in Table 1 into JSON format through .sheet_to_json()
//Data in table directory one let worksheet = [[0]]; //Convert data into json data format data = .sheet_to_json(worksheet);
6. Convert the read JSON data into data that can be passed to the server.
//Change the read data into data that can be passed to the server (location =>address, school name =>shcoolName...) let arr = []; ((item) => { let obj = {}; for (let key in character) { // hasOwnProperty() method returns a Boolean value indicating whether the object's own properties have the specified properties (that is, whether there is a specified key) // Even if the value of the property is null or undefined, hasOwnProperty will still return true as long as the property exists. // Determine whether there are no specified attributes and terminate the execution if (!(key)) break; let v = character[key], text = , type = ; v = item[text] || ""; //Convert data to the corresponding data type. If it does not match, do not do any operation. type === "string" ? (v = String(v)) : null; type === "number" ? (v = Number(v)) : null; obj[key] = v; } (obj); }); // Give the user a little delay await delay(100); //Display the imported data on the page = arr;
Export
// 1. Convert data into table names corresponding to let arr = ((item) => { return { School level: , Remark: , School name: , location: , }; }); //2. Used to convert JSON data into cell data in Excel worksheets. let sheet = .json_to_sheet(arr); //3. Used to create a new Excel workbook object It returns a blank workbook to which worksheet and cell data can be added. let book = .book_new(); //4. Used to add a new worksheet to an existing Excel workbook object (Workbook) // book_append_sheet(wb, sheet, name=None) //The parameter wb is the Workbook object to which the worksheet is to be added; the sheet parameter is the Worksheet object to which the name parameter is to be specified for the worksheet (if not specified, the default name is used) .book_append_sheet(book, sheet, "sheet1"); //5. Used to download Excel workbooks to the local file system Eceial table to be downloaded by the book. The second parameter is the table name (book, `user${new Date().getTime()}.xls`);
This is the end of this article about the example of import and export of Vue tables. For more related vue excel import and export content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!