Excel import function (Ajaxfileupload), ajaxfileupload

Source: Internet
Author: User

Excel import function (Ajaxfileupload), ajaxfileupload

Preface:
Front-end implementation using Easyui + Ajaxfileupload
The backend adopts the springmvc framework, in which the parsing xml is encapsulated into a jar package and can be directly called.

Preparation:
Front-end need to import (easyui import js omitted, self-import can be) Ajaxfileupload. js: http://files.cnblogs.com/files/holdon521/ajaxfileupload.zip
The backend needs to be imported:
Ht-excel.jar: http://files.cnblogs.com/files/holdon521/ht-excel.zip

Front-end code:
<Div class = "easyui-layout" fit = "true"> function ajaxFileUploadForType () {if ($ ('input [type = "file"] '). val ()! = "") {Var extend = $ ('input [type = "file"] '). val (). substr ($ ('input [type = "file"] '). val (). lastIndexOf (". ") + 1); if (" xls | xlsx ". indexOf (extend + "|") =-1) {flagPic = false; $. messager. alert ("prompt message", "the selected file must be an EXCEL file. Please confirm! ");} Else {$. ajaxFileUpload ({url: 'uploadtvtype', // secureuri: false, // whether security protocol is required. This parameter is generally set to false fileElementId: 'file ', // The ID dataType: 'text' of the file upload field. // the return value type is set to json success: function (responseJSON) // The server responds to the processing function {if (responseJSON = "0") {$. messager. alert ('prompt info', 'TV station type imported successfully! '); $ Ht. win. close ('uploadtvtype'); $ ht. grid. reload ('gridtvtype');} else if (responseJSON = "2") {$. messager. alert ('prompt info', 'no data that meets the requirements or the data to be imported already exists in the Database. Please confirm! ');} Else if (responseJSON = "error") {$. messager. alert ('prompt information',' TV station type import failed! ');} Else {$. messager. alert ('prompt information', responseJSON); $ ht. grid. reload ('gridtvtype') ;}}, error: function (data, status, e) // handler for server response failure {$. messager. alert ('prompt info', 'TV station type import failed! ') ;}}} Else {$. messager. alert ("prompt message", "select an EXCEL file! ") ;}}</Script>

Backend code:

Note: The object class corresponds to the column name in Excel. You can set a non-null check (if it is null, this row of data cannot be obtained by default). Just rewrite the parent method. The following code comment is rewritten.


Import java. util. List;


Import com. sh. excelUtil. model. BaseModel;
Import com. sh. excelUtil. persistence. FieldNote;


Public class TvTypeBean extends BaseModel implements java. io. serializable {private static final long serialVersionUID =-5217032742530006972l; @ FieldNote (logicalName = "terminal type", physicalName = "terminalType", notNull = true) private String terminalType; @ FieldNote (logicalName = "Classification code", physicalName = "tvTypeCode", notNull = true) private String tvTypeCode; @ FieldNote (logicalName = "Classification name", physicalName = "tvTypeName ", notNull = true) private String tvTypeName; @ FieldNote (logicalName = "Classification abbreviation name", physicalName = "tvTypeAbridgeName", notNull = true) private String tvTypeAbridgeName; @ FieldNote (logicalName = "sorting field", physicalName = "orderBy") private String orderBy; @ Override public List <Object> check (BaseModel model, List <Object> valueList) {// valueList = super. check (model, valueList); // TODO: return valueList when the data is not empty;}/*** @ return the terminalType */public String getTerminalType () {return terminalType;}/*** @ param terminalType the terminalType to set */public void setTerminalType (String terminalType) {this. terminalType = terminalType;}/*** @ return the tvTypeCode */public String getTvTypeCode () {return tvTypeCode ;} /*** @ param tvTypeCode the tvTypeCode to set */public void setTvTypeCode (String tvTypeCode) {this. tvTypeCode = tvTypeCode;}/*** @ return the tvTypeName */public String getTvTypeName () {return tvTypeName ;} /*** @ param tvTypeName the tvTypeName to set */public void setTvTypeName (String tvTypeName) {this. tvTypeName = tvTypeName;}/*** @ return the tvTypeAbridgeName */public String getTvTypeAbridgeName () {return tvTypeAbridgeName ;} /*** @ param tvTypeAbridgeName the tvTypeAbridgeName to set */public void setTvTypeAbridgeName (String tvTypeAbridgeName) {this. tvTypeAbridgeName = tvTypeAbridgeName;}/*** @ return the orderBy */public String getOrderBy () {return orderBy ;} /*** @ param orderBy the orderBy to set */public void setOrderBy (String orderBy) {this. orderBy = orderBy ;}}

 

Import java. util. List;

Import com. sh. excelUtil. excel. POIExcelUtil;

@ Controller
@ RequestMapping ("/channelinfomanage ")
Public class ChannelInfoManageController {

    
@ RequestMapping ("/uploadTvType") public void uploadTvType (HttpServletRequest request, @ RequestParam ("file") MultipartFile file, HttpServletResponse response) throws Exception {// This object is a tool class POIExcelUtil poi = new POIExcelUtil (); poi. impExcelFile (fileName, new TvTypeBean (), inputStream); List <Object> valueList = poi. getValueList ();

If (valueList! = Null & valueList. size ()> 0 ){
For (int I = 0; I <valueList. size (); I ++ ){

TvTypeBean bean = (TvTypeBean) valueList. get (I );

// Output the field data and insert it into the corresponding table.

System. out. println (bean. getTvTypeCode () + .......);

}

}

    }  }}

 

 

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.