Front-desk JSP
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Java code, using a package of Poi3.5,commons-io2.1import Java.io.file;import Java.io.ioexception;import Java.io.inputstream;import Javax.servlet.http.httpservletrequest;import Org.apache.commons.io.fileutils;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.xssf.usermodel.xssfcell;import Org.apache.poi.xssf.usermodel.xssfrow;import Org.apache.poi.xssf.usermodel.xssfsheet;import Org.apache.poi.xssf.usermodel.xssfworkbook;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.multipart.multipartfile;@ Controllerpublic class Hdimporcontroller {@RequestMapping ("/initloandata") Public String Initloandata ( HttpServletRequest request) {return "Views/seRvice/import/loandata ";} @RequestMapping (value = "/loandata", method = requestmethod.post) public String loandata (@RequestParam multipartfile[] Myfiles,httpservletrequest request) throws IOException {//If only one file is uploaded, only the Multipartfile type is required to receive the file. And there is no need to explicitly specify @requestparam annotations//If you want to upload multiple files, you will need to use the multipartfile[] type to receive the file, and also specify @requestparam annotations//and upload multiple files, all < in the foreground form The name of the input//type= "file"/> should be myfiles, otherwise the myfiles in the parameter cannot get to all uploaded files file[] files = new File[myfiles.length];for ( Multipartfile myfile:myfiles) {if (Myfile.isempty ()) {System.out.println ("File not uploaded");} else {System.out.println ("File Length:" + myfile.getsize ()); System.out.println ("File type:" + myfile.getcontenttype ()); System.out.println ("File name:" + myfile.getname ()); System.out.println ("File formerly known as:" + myfile.getoriginalfilename ()); System.out.println ("========================================");//If you are using a TOMCAT server, the files will be uploaded to \\%tomcat_home%\\ Webapps\\yourwebproject\\web-inf\\upload\\ folder in string Realpath = Request.getsession (). Getservletcontext (). Getrealpath ("/files/upload/loandata ");//There is no need to deal with IO stream shutdown, because the Fileutils.copyinputstreamtofile () method will automatically shut down the IO stream used, I am the source of the code to see that the file File = new file ( Realpath, Myfile.getoriginalfilename ()); Fileutils.copyinputstreamtofile (Myfile.getinputstream (), file), if (Myfile.getoriginalfilename (). ToLowerCase (). EndsWith ("xls")) {Readxls (Myfile.getinputstream ());} Else{readxlsx (file+ "");}}} return "Views/service/import/loandata";} private void Readxlsx (String fileName) throws IOException {//string fileName = "d:\\excel\\xlsx_test.xlsx"; Xssfworkbook Xssfworkbook = new Xssfworkbook (fileName);//Cycle sheet sheetfor (int numsheet = 0; Numsheet < XSSFWORKBOOK.GETN Umberofsheets (); numsheet++) {Xssfsheet Xssfsheet = Xssfworkbook.getsheetat (Numsheet); if (Xssfsheet = = null) {continue;} Loop line rowfor (int rowNum = 0; RowNum <= xssfsheet.getlastrownum (); rownum++) {Xssfrow Xssfrow = Xssfsheet.getrow (rowNum ); if (Xssfrow = = null) {continue;} Cyclic column cellfor (int cellnum = 0; Cellnum <= xssfrow.getlastcellnum (); cellnum++) {Xssfcell Xssfcell = Xssfrow.getCell (Cellnum); if (Xssfcell = = null) {continue;} System.out.print ("" + GetValue (Xssfcell)); System.out.println ();}}} @SuppressWarnings ("static-access") Private String GetValue (Xssfcell Xssfcell) {if (xssfcell.getcelltype () = = Xssfcell.cell_type_boolean) {return string.valueof (Xssfcell.getbooleancellvalue ());} else if (Xssfcell.getcelltype ( ) = = Xssfcell.cell_type_numeric) {return string.valueof (Xssfcell.getnumericcellvalue ());} else {return string.valueof (Xssfcell.getstringcellvalue ());}} private void Readxls (InputStream is) throws IOException {Hssfworkbook Hssfworkbook = new Hssfworkbook (IS);//Cycle Sheet SHEETFO R (int numsheet = 0; Numsheet < hssfworkbook.getnumberofsheets (); numsheet++) {Hssfsheet Hssfsheet = Hssfworkbook.getsh Eetat (Numsheet); if (Hssfsheet = = null) {continue;} Loop line rowfor (int rowNum = 0; RowNum <= hssfsheet.getlastrownum (); rownum++) {Hssfrow Hssfrow = Hssfsheet.getrow (rowNum ); if (Hssfrow = = null) {continue;} Circular column cellfor (int cellnum = 0; Cellnum <= hssfrow.geTlastcellnum (); cellnum++) {Hssfcell Hssfcell = Hssfrow.getcell (Cellnum); if (Hssfcell = = null) {continue;} System.out.print ("" + GetValue (Hssfcell)); System.out.println ();}}} @SuppressWarnings ("static-access") Private String GetValue (Hssfcell Hssfcell) {if (hssfcell.getcelltype () = = Hssfcell.cell_type_boolean) {return string.valueof (Hssfcell.getbooleancellvalue ());} else if (Hssfcell.getcelltype ( ) = = Hssfcell.cell_type_numeric) {return string.valueof (Hssfcell.getnumericcellvalue ());} else {return string.valueof (Hssfcell.getstringcellvalue ());}}}
Spingmvc uploading files, poi parsing xls,xlsx