Last time we used the POI technology to complete the printing of the user list, let's complete the import of the user list Excel file.
We are going to import this Excel file into our system:
Specifically, we're going to import data from Excel into the database and save it.
Our operating procedures are:
1. Get Excel Files
2. Import
2.1. Read workbook
2.2. Read the worksheet
2.3, read the line
2.4. Read cells
2.5. Save the user
Let's do a concrete implementation of the work.
We can see an "Export" button in the JSP page of the user list.
<input type= "button" value= "Export" class= "S_button" onclick= "Doexportexcel ()"/> <input name= "Userexcel" type= " File "/>
We set a click event for this button, use JS to link it to our import path, and jump to the appropriate action to import the work:
Import List function Doimportexcel () {document.forms[0].action= "${basepath}tax/user_importexcel.action";d ocument.forms [0].submit ();}
We add the Importexcel export method in Useraction:
Import user list public String importexcel () {//1, get the Excel file if (Userexcel!=null) {//Is Excelif (userexcelfilename.matches ("^.+ \\. (? i) ((xls) | (xlsx)) $ ")) {//2, Import userservice.importexcel (Userexcel,userexcelfilename);}} return "list";}
We add the Importexcel export method to the UserService interface, and then implement this method in Userserviceimpl:
The preceding three attributes are added, and the get and set methods private File userexcel;private string Userexcelcontanttype;private string userexcelfilename;@ overridepublic void Importexcel (File userexcel, String userexcelfilename) {try {fileinputstream FileInputStream = new Fil Einputstream (userexcel);//Determines whether the 03 version of Excel (or 07) is a Boolean is03excel = Userexcelfilename.matches ("^.+\\.") i) (XLS) $ "),//1, read workbook workbook workbook = Is03excel? New Hssfworkbook (FileInputStream): New Xssfworkbook (FileInputStream);//2, reading sheet sheet sheet=workbook.getsheetat (0); /3, read line if (Sheet.getphysicalnumberofrows () >2) {User user=null;for (int i = 2; I < sheet.getphysicalnumberofrows (); I + +) {//4, Read cell row row=sheet.getrow (i); user=new user ();//Username cell Cell1=row.getcell (0); User.setname ( Cell1.getstringcellvalue ());//Account cell Cell2=row.getcell (1); User.setaccount (Cell2.getstringcellvalue ());// Department cell Cell3=row.getcell (2); User.setdept (Cell3.getstringcellvalue ());//Gender Cell Cell4=row.getcell (3); User.setgender (Cell4.getstringcellvalue (). Equals ("male"));//e-mail cell cell5=Row.getcell (4); User.setemail (Cell5.getstringcellvalue ());//import user's initial password is 123456user.setpassword ("123456");// The default user state is valid user.setstate (user.user_state_valid),//5, save user Userdao.save,}} Workbook.close (); Fileinputstream.close ();} catch (Exception e) {e.printstacktrace ();}}Let's restart the server to test:
The previous list of users is only four people:
Then we click Import and import our prepared import file:
After clicking OK, the Discovery list has more data that we previously imported:
Explain how our import method is done!
The "import" and "export" functions of our user list are all written!
Reprint Please specify source: http://blog.csdn.net/acmman/article/details/49401251
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"SSH Project Combat" national tax cooperation platform-7. POI Import User list file