This content mainly includes (file upload, excel2003 Data Import Database) Excel Import Database functionality requires JXL JAR package support
Here is the front-end test code for file uploads
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%> <%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body> <formAction= "Getallbyexcel"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "File" > <inputtype= "Submit"value= "Upload"> </form></Body></HTML>
The following control class is used for uploading Excel files to the server
@SuppressWarnings ("Unchecked") @RequestMapping (value= "Getallbyexcel", method =requestmethod.post) PublicObject getallbyexcel (httpservletrequest request,httpservletresponse response, Modelmap model) {Multiparthttpse Rvletrequest mulltipartrequest=(multiparthttpservletrequest) request; Multipartfile Files=mulltipartrequest.getfile ("File"); //get the Upload server pathString path=request.getsession (). Getservletcontext (). Getrealpath ("/web-inf/res/upload"); String FileName=Files.getoriginalfilename (); Try{InputStream InputStream=Files.getinputstream (); byte[] B =New byte[1048576]; intLength =Inputstream.read (b); Path+= "\\" +FileName; //file stream writes to server sideFileOutputStream OutputStream =NewFileOutputStream (path); Outputstream.write (b,0, length); Inputstream.close (); Outputstream.close (); //parsing ExcelList<map<string, object>> lists=commonutil.getallexcel (path);
This feature requires importing the JXL related jar package, which provides an Excel path to return list<map<string, object>>
//Get Excel Data Public StaticList<map<string, object>>getallexcel (String url) {List<map<string, object>> models=NewArraylist<map<string, object>>(); Try{File Filse=NewFile (URL); Workbook RWB=Workbook.getworkbook (Filse); Sheet Sheet=rwb.getsheet (0); intClos=sheet.getcolumns ();//get number of columns intRows=sheet.getrows ();//Get row Countstring[] Heads=NewString[clos];//Save all key values for(inti = 0; i < rows; i++) {Map<string, object> MP =NewHashmap<string,object>(); for(intj=0;j<clos;j++){ if(i = = 0) {Heads[j]= Sheet.getcell (J, i). getcontents ();//get all key values } Else{mp.put (heads[j], Sheet.getcell (J, i). getcontents ());//Get value per row value } } //Add to list collection if map is not empty if(!Mp.isempty ()) {Models.add (MP); } } } Catch(Exception e) {//Todo:handle Exception } returnmodels; }
Record-java (JXL) Excel Import Database