Java import excel ideas and code examples
Export is to convert list to excel (listtoexcel)
Import is to convert Excel to list (Excel to list)
1、 Train of thought analysis
1. We need to import, that is, upload the file first, and then read the data of the file.
2. We need to have an imported template, because the imported excel column should match our data fields, so we need to give it a provision, that is, the template.
3. According to our company's routine, a temporary table for importing information is made to store the information in the imported file. When importing, we first clear the table information, then get the data and put it in. Then we check the imported data, and finally import all the data.
The purpose of this is to prevent the imported data and columns from being directly imported into the library without being aligned. If this is true, it will be very embarrassing. We made three buttons, one import, one confirm import, and one import template download.
4. Process:
Click the batch import button to jump to the import page.
Click template download. (write a template. XLS file in advance, and click this button to place the download link)
Enter data in the template.
Click the import button to pop up the file upload mode box.
Select file and upload the file.
After uploading successfully, the background method is called to read the contents of the file.
The content is generated into a table and displayed on the import page.
Observe whether there is any error in the data.
Click OK to import, call the background method and execute the import operation.
Callback function, prompt of import success / import failure.
2、 Code analysis
1. Front end JS code:
2. Background function code
There are four requests in the front end. One is to initialize the page data loading. Of course, there is no data at the beginning; Upload an import file; A confirmation import; After an import is completed, the page jumps back to the information page (the information page has a batch import page that jumps to this import page).
I won't talk about the last one after the first one. Tell me about the second and third.
① second
② third
There's nothing to say... You should be able to understand what you will, and I won't understand what you can't understand
3、 Result diagram