Record-java (JXL) Excel Import Database

Source: Internet
Author: User
Tags import database

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

Related Article

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.