The Excel file imports data (using the jxl. jar package) and exceljxl. jar according to the specified template.

Source: Internet
Author: User

The Excel file imports data (using the jxl. jar package) and exceljxl. jar according to the specified template.
The method in this article is only applicable to Excel2003. It is best to use poi to read Excel2007. jar, it is said that poi. jar is still being updated, jxl. jar is no longer updated. It is best to learn about poi for reading and writing Excel files. jar. jar usage. You can use the jxl. jar and poi. jar packages to read the content in an Excel file. Here, we will only introduce how to use the jxl. jar package. First import jxl. jar package. The framework used in this example is MyBatis + Spring MVC. To avoid security issues such as uploading a virus file to a local machine, the file import function generally does not allow reading the local path of the file, therefore, to read the content of a local file, you must first upload the file to the server and then read it from the server path. Therefore, the process of importing Excel file data is as follows: (1) uploading an Excel file to the server. (2) Use the jxl. jar package to read the data of the Excel file stored in the server path. (3) Save the read data to the object and insert it into the database. Step 1: File Upload: (1) Add the attribute enctype = "multipart/form-data" to the form on the jsp page ". <Form: form id = "" modelAttribute = "" action = "" method = "post" enctype = "multipart/form-data" class = ""> (2) the form contains a file component: <input type = "file" id = "" name = ""/> if you want to click "file Import", the SELECT file window is displayed, if you do not want to display the file input, you can set the input to hidden. When you click the button, the function is triggered and the click Event of the file input is set in the triggered function. For example: <% @ page contentType = "text/html; charset = UTF-8" %> <% @ include file = "/WEB-INF/views/include/taglib. jsp "%> VarExtStart = filepath. lastIndexOf (". "); fileType = filename. substring (extStart, filename. length); to replace var fileType = filename. match (/^ (. *)(\.) (. {}) $/) [3]; // find the file suffix var isExist = false; for (I in array) {if (fileType. toLowerCase () = array [I]. toLowerCase () {isExist = true; return true ;}} if (isExist = false) {alert ("incorrect file type"); return false ;} return true; }}</script> PublicString importExcel (@ RequestParam (value = "excelFile", required =False) MultipartFile file, RedirectAttributes redirectAttributes, HttpServletResponse response) {InputStream FCM =Null;String uploadPath = SystemPath.GetSysPath() + "WEB-INF/views/app/upload" + File.Separator; String fileName = file. getOriginalFilename (); // file name String path = uploadPath + fileName. substring (0, fileName. lastIndexOf (".") + DateTimeUtil.Get_YYYYMMDDHHMMSS(NeDate () + fileName. substring (fileName. lastIndexOf (". "), fileName. length (); // to save duplicate files on the server, modify the File name and add the time File uploadFile =NewFile (path );Try{File. transferTo (uploadFile); // upload the content in the local file to the Server file.NewFileInputStream (path); Workbook wb = Workbook.GetWorkbook(FS); // how to read the Excel file content in jxl. jarIf(! JudgeExcelModel (wb) {// judgeExcelModel is used to determine the Excel file format. You can add FileUtils as needed.DeleteFile(Path); addMessage (redirectAttributes, "Excel format error, please download the template provided by the platform ");}Else{IntSheet_size = wb. getNumberOfSheets (); // Number of work ColumnsFor(IntIndex = 0; index <sheet_size; index ++) {Sheet sheet = wb. getSheet (index); // content in the work barFor(IntI = 1; I <sheet. getRows (); I ++) {Student stu =NewStudent (); String age = sheet. getCell (0, I ). getContents (); String name = sheet. getCell (1, I ). getContents (); String class = sheet. getCell (2, I ). getContents (); String school = sheet. getCell (3, I ). getContents (); stu. setAge (age); stu. setName (name); stu. setClass (class); stu. setSchool (school); stu Service. insert (stu) ;}} addMessage (redirectAttributes, "batch import data successful"); FileUtils.DeleteFile(Path); // delete the files on the server after the import is successful }}Catch(IllegalStateException e) {e. printStackTrace ();}Catch(BiffException e) {e. printStackTrace ();}Catch(IOException e) {e. printStackTrace ();}Finally{If(FS! =Null){Try{FCM. close ();}Catch(IOException e) {e. printStackTrace () ;}}}here, the Excel file data import function has been completed. In specific applications, you should also verify the data inserted into the database, place the records that pass the verification in the list set, and insert the data that passes the verification into the database after all record verification is completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.