Spingmvc uploads files, poi parses xls, xlsx

Source: Internet
Author: User

Spingmvc uploads files, poi parses xls, xlsx

Foreground jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
 Data Import <Script type = "text/javascript"> $ (function () {$ ("# btn_check "). click (function () {$ ("# myfiles "). trigger ('click') ;}); $ ("# filePath "). click (function () {$ ("# myfiles "). trigger ('click') ;}); </script>
 
Java code, the package used is 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, you only need to receive the file in the MultipartFile type, in addition, you do not need to explicitly specify the @ RequestParam annotation // If You Want To upload multiple files, you must use the MultipartFile [] type to receive files, also specify the @ RequestParam annotation // when uploading multiple files, allThe names of all files must be myfiles. Otherwise, myfiles In the parameter cannot obtain 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 name:" + myfile. getOriginalFilename (); System. out. println ("========================================== = "); // If the Tomcat server is used, the file will be uploaded to \ % atat_home % \ webapps \ YourWebProject \ WEB-INF \ upload \ String realPath = request. getSession (). getServletContext (). getRealPath ("/files/upload/loanData"); // you do not have to handle the issue of IO stream shutdown because FileUtils. the copyInputStreamToFile () method will automatically turn off the IO stream used internally. I only know 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); // cyclic worksheet Sheetfor (int numSheet = 0; numSheet <xssfWorkbook. getNumberOfSheets (); numSheet ++) {XSSFSheet xssfSheet = xssfWorkbook. getSheetAt (numSheet); if (xssfSheet = null) {continue;} // looping row Rowfor (int rowNum = 0; rowNum <= xssfSheet. getLastRowNum (); rowNum ++) {XSSFRow xssfRow = xssfSheet. getRow (rowNum); if (xssfRow = null) {continue;} // The repeating 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); // cyclic worksheet Sheetfor (int numSheet = 0; numSheet 


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.