1. Front-end-led implementation stepsThe first step is to click the import button on the page to read the Excel file Completed using plugins. The second step is to process the data format according to the requirements of the backend and convert it into the format they need You need to write the logic yourself. The third step is to send the data back via ajax through the backend interface Adjust the interface and perform normal operations. In short: the front end reads the Excel file, modifies the file format, and calls the interface 2. Read Excel filesNote: This step can realize the front-end import function. If you are interested in modifying the format, you can continue to step 3. Summary: Copy the code to your own folder and download the necessary plugins. 2.1 The upload method in vue-admin-element provided by elementUI is used (Baidu warehouse cloning method, the official website also has it) 2.2 Download the package npm install xlsx -S (by default, step 2.1 has been completed) 2.3 Introduce the UploadExcel component and register it as global (find If you don’t know how to register global components, refer to the following code: (You can do it in main.js if you like, but I will do it here for high maintainability) import PageTools from './PageTools' import UploadExcel from './UploadExcel' export default { // Initialization of the plugin. The global functions provided by the plugin can be configured here install(Vue) { // Global registration of components Vue.component('PageTools', PageTools) // Register toolbar component Vue.component('UploadExcel', UploadExcel) // Register imported excel component } } 2.4 Introduce components, use components, configure routes, and set click callback functions (no need to say more) 2.5 Test results Manually enter the routing address you set in the browser and the page will jump to it Summary: 1. The most important thing is to copy and then download the necessary plug-ins 2. It is necessary to introduce the UploadExcel component (to import Excel files), but it is not necessary to register it globally, depending on your mood 3. Import the component and register the callback function. The two parameters in it are required. You can refer to the source code for easier understanding. 4. The essence of the Excel import plug-in: convert Excel into regular data that can be recognized by JS after analysis, and we can perform any operation on the data 3. Process the dataNote: This step actually tests your ability to use JavaScript. Unfortunately, this is not difficult for me, and I believe it will not be difficult for me for a long time. 3.1 Format required by the backend: 3.2 What we are going to process:
3.3 Encapsulate a processing function separately: /** * results excel table content // [ {'name': 'Xiao Zhang', 'mobile phone number': '13712345678'}, {.....} ] // Target// [ {'username': 'Xiao Zhang', 'mobile': '13712345678'}, {.....} ] */ transExcel(results) { const userRelations = { 'Job Entry Date': 'timeOfEntry', 'Mobile number': 'mobile', 'Name': 'username', 'Correction Date': 'correctionTime', 'work number': 'workNumber', 'Department': 'departmentName', 'Form of employment': 'formOfEmployment' } return results.map(item => { const obj = {} // 1. Get all the attribute names of this object: ['name', 'phone number'] // 2. Traverse this array, find the corresponding English name in userRelations through the Chinese name, and save the value const zhKeys = Object.keys(item) zhKeys.forEach(zhKey => { const enKey = userRelations[zhKey] // If it is in time format, convert it if (enKey === 'correctionTime' || enKey === 'timeOfEntry') { obj[enKey] = new Date(formatExcelDate(item[zhKey])) } else { obj[enKey] = item[zhKey] } }) return obj }) } handleSuccess({ results, header }) { console.log('The content read from the current excel file is', results) // results: [{Joining date: 44502, Name: xxxx}] // Target: // results: [{timeOfEntry: 44502, username: xxxx}] // Process the format read from Excel const arr = this.transExcel(results) console.log('The format after conversion is', arr) }) 3.4 Date processing functions: // Convert the date format in the Excel file back to standard time // https://blog.csdn.net/qq_15054679/article/details/107712966 export function formatExcelDate(numb, format = '/') { const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000 - 8 * 3600000) time.setYear(time.getFullYear()) const year = time.getFullYear() + '' const month = time.getMonth() + 1 + '' const date = time.getDate() + '' if (format && format.length === 1) { return year + format + month + format + date } return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date) } 4. Call the interface, send a request, and use it in the pageimport { importEmployee } from '@/api/employees' export default { name: 'Import', methods: { async handleSuccess({ results, header }) { try { console.log('The content read from the current excel file is', results) // results: [{Joining date: 44502, Name: xxxx}] // Target: // results: [{timeOfEntry: 44502, username: xxxx}] const arr = this.transExcel(results) console.log('The format after conversion is', arr) //Call the upload interface, const rs = await importEmployee(arr) console.log('Call the upload interface', rs) // After the upload is successful, return to the previous page this.$router.back() this.$message.success('Operation successful') } catch (err) { this.$message.error(err.message) } } } 5 Summary:The import function is similar to the export function. The difficulty lies in converting the data format. This article does not introduce it in detail, but it is explained in detail in another export article. If you are interested, you can go and have a look. Click here to jump to the export function implementation This is the end of this article about Vue’s implementation of the Excel import function. For more relevant Vue import Excel content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Practical method of deleting associated tables in MySQL
Overview In actual business scenario applications...
1. Environment and preparation 1. Ubuntu 14.04 2....
Quick solution for forgetting MYSQL database pass...
Preface DISTINCT is actually very similar to the ...
MySQL deduplication methods 【Beginner】There are v...
Good morning everyone, I haven’t updated my artic...
I can log in to MYSQL normally under the command ...
<br />Tips for making web table frames. ----...
Preface Recently, when working on a high-availabi...
1. Download the installation package from the off...
First method Alibaba Cloud and Baidu Cloud server...
First, let's simulate the data coming from th...
Preface This article records a problem I encounte...
Currently implemented are basic usage, clearable,...
Table of contents 1. v-text (v-instruction name =...