Excel spreadsheet data import

开发工具与关键技术:vs2015  MVC
作者:陈星宇
撰写时间:2019.5.1

When we learn to add, is a new piece of data, when we need a lot of data, we wish to increase and that is something new import feature, we talk about the process of data import Excel spreadsheet.
Excel spreadsheet import, in the final analysis a file upload function, the Excel spreadsheet upload this folder to process our controller and processed to obtain the data inside. Next we talk about the details of the process to import Excel spreadsheet.
First, we have to click on input trigger a click event tag in the file attributes button to open the window file upload
Here Insert Picture Description
file selection Excel spreadsheet you need and upload to the input tag in the form tag. As shown:
Here Insert Picture Description
so that we can get Excel spreadsheet upload come in single form,
then we can submit the form by means of form data to the controller for processing data
where we get to get by id form form,
the method of our controller ImportExcel a defined manner, and a post submission,
Here Insert Picture Description
the data file will be submitted to a ImportExcel here we defined in the controller are
Here Insert Picture Description
here in particular point to note here acts as the base class must be HttpPostedFileBase, and pass over the parameters must be equal to the form of the name in the form of name, and here we are here to give the name value is to file
uploaded to the controller, whether we need to do first thing is to determine pass over this file is Excel form
we will get file type of the file by the if statement to determine
Here Insert Picture Description
if a pass over the Excel spreadsheet file, we also need to determine whether this file has a data transfer because an empty meaningless data
we declare a binary data, and then read the contents of the documents to the two Binary array, then the array is determined whether the data is empty
Here Insert Picture Description
Here we judge for Excel data table is over
the field we need to query the contents of the required form, then declare an entity class list, used to store imported data.
In addition we also need to put data into binary data in a DataTable file stream
Here Insert Picture Description
here through the loop with the data in each row are check out
we just declare an entity class list, here we have to declare the same a means to import data entity class, then for each class into a list of questions we declare classes of questions
Here Insert Picture Description
here we use the header text as a query, obtaining the Name and ID data
Here Insert Picture Description
from FIG seen, our query the results are placed in the student entity class
and then we will use methods Add new entity classes to the entity class list
Here Insert Picture Description
when this step after the operation is completed, the data on our Excel spreadsheet upload is complete, we can put data into the Session
Here Insert Picture Description
when the need to deposit into the database, direct access to data in the Session, through the familiar new method you can put different data in different database tables. Here to import an Excel spreadsheet is over.

Guess you like

Origin blog.csdn.net/tianxiu11/article/details/89974139