How to import Excel spreadsheet

Development tools and key technologies: VS MVC
Author: Lee Silver
Write Time: May 3, 2019

You must first prepare an Excel spreadsheet template before you import an Excel spreadsheet, and then put it in the designated place waiting to use.
The first step to import Excel modal pop-up box:
Here Insert Picture Description
pop-up modal box of course, is to reset the form, because the form is a modal box thing! Then disable button to save (light blue button) database: import into the database is the final step in order to avoid operational errors, so now to have it disabled (later to be enabled). Then declare a global variable to place the temporary tables, import data reinitialization temporary table, empty the temporary tables, data overload; finally modal popup box.
The second step download import templates :( only a code)
to open a new window and load the page URL address specified in the window.
. window open ( "DownImportTemplate") ;
finish these two steps, you write to the controller controller code download template:
Here Insert Picture Description
First, get a template path (create a new folder in the project, ready to go into the template) and then locate the file path to determine what this template exists, template exists to get the file name and then returns the file (this has been achieved download function), the template does not exist prompts the user: template file does not exist, contact the system operation and maintenance personnel.
After downloading the realization of the function, then put the data in Excel spreadsheet uploaded to a temporary table. (There are two steps: The first step is to select the operation data in the Excel spreadsheet to save session inside it, the second step is from the session data extracted from inside the paging process it and then return it to the table). Select the file, upload the file will involve change event of the form, so give this form to write a change change event.
Look at the code:
Here Insert Picture Description
First, in order to avoid triggering after deselecting files to upload, so to determine whether the form is empty; empty is returned, otherwise continue.
Then save the data to the controller write Excel spreadsheet imported into the session code: First look at the session empty, so the next import Excel spreadsheet else is the original data is not stored inside.
And then determine whether the page pass over the file as an Excel spreadsheet (need to get the file extension), if the acquired file name suffix specified file type extension conversion (regardless of capitalization) will file as a binary array, otherwise to remind the user of an incoming file type error, please upload an Excel file. The file is converted to a binary array must first declare an array to store the binary file, then the incoming file into a binary array into fileBytes; then it should turn into a binary array memory stream, and finally the memory stream into a workbook .
Here Insert Picture Description
In fact, four steps:
1, to obtain the document reading;
2, converts the file to a binary array;
3, into the memory array to a binary stream;
4, using NPOI the data stream read into memory Excel.
This time the workbook has been in existence, but whether there is a worksheet in this workbook is not known, so it is necessary to determine whether there is a worksheet about this workbook.
Here Insert Picture Description
To obtain the corresponding ID based on the name; declarative object list and then, the information stored in the import list, then it would obtain the workbook to the first sheet.
Now we get to the worksheet, but is not known whether the worksheet data, we still need to determine whether there is data in the worksheet. If no data is returned empty, if there is data, then it processes the data, i.e. the obtained data to the DataTable installed (data table).
DataTable put data into the premise that this table already exists, so the first definition of a dataTable: first obtain the header line, then number of columns in the table, and finally get the number of rows in the table. DataTable create columns, each cell in circulation adding header row of data to be used for loop, each cell cycle traversal header row, and the acquired data of each cell header row, and the acquired title rows of data into a dataTable.
Here Insert Picture Description
After studying the data into the data dataTable to Excel, Excel is the first line of the title, is a for loop to read data. Acquired in the for loop row (second row) of the data, the data in this row into dataTable, then the received data to create DataTable row line, and then determines whether empty, not empty then traverse each row in excel cells, wherein the new row is added to the dataTable go.
DataTable data has been put in, we need to put this data dataTable in order, then put the list of objects declared before the inside, so that the data will be saved in the session to the format of the column.
First, declare two variables (ImportSuccess ++ ;, ImportFail ++;) , records the number of successful and failed; then write a foreach loop, data traversal cycle dataTable in, and then create an object to hold each piece of data. Details, please read the code: Here Insert Picture Description
Due to abnormal prone code here, so to try ... catch catch them abnormal. Inside the professional name, grade and class names are the names Excel table of contents in need, in fact, there are college name, student number, etc., not list them here, mainly because too many codes.
After acquiring the data to each piece of data will be added to the list of objects:
listStudentVo.Add (Student);
Importsuccess ++; this variable will be added successfully self plus one.
ImportFail ++; this variable will fail to add a self-imposed.
After performing the cycle all saved, the final list of data obtained in the session:
Here Insert Picture Description
if after the completion of the page to return back to the controller performs a value close load layer is then passed over a determined state value is true; true on the restart button to reload the data and the database; otherwise empty lattice form, the output value.
After saving the data to the session, the session data will be extracted and put it paging. Finally, save the Excel spreadsheet data import into a database.
First, open the loading layer, and a request to save the URL data imported, and saving the data into the database. But before saving data to a database to determine what has been imported data in the database. If a piece of data in the database already exists, the article data can not be saved to the database. If the imported data did not repeat then save the imported data with data in the database.
Here Insert Picture Description
And then determine whether the new data is zero. If zero, the new failure; not zero, then saved successfully.
Here Insert Picture Description
After successfully saved back to the page, off loading and modal layer frame, then prompts the user, the successful introduction. Finally refresh the table, the data successfully imported displayed.
Note: The above code from the instructor of the class project.

Guess you like

Origin blog.csdn.net/weixin_44560796/article/details/89813462