前言:
前端采用Easyui+Ajaxfileupload實現
后端采用sPRingmvc框架,其中把解析xml封裝成了一個jar包,直接調用即可準備:
前端需要導入(easyui導入js省略,自行導入即可)Ajaxfileupload.js 下載地址:http://files.VEVb.com/files/holdon521/ajaxfileupload.zip后端需要導入:ht-excel.jar 下載地址:http://files.VEVb.com/files/holdon521/ht-excel.zip
前端代碼:
<div class="easyui-layout" fit="true" style="overflow-y:auto;"> <div class="ht-info"> <div class="ht-tip icon-tip"></div> <div>電視臺類型導入</div> </div> <div> <form id="uploadform" action="" method="post" enctype="multipart/form-data"> <table> <tr> <td>請選擇文件:</td> <td> <input type="file" name="file" id="file" ></input> </td> </tr> </table> </form> </div> <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;background:#EAF2FF;"> <a href="#" onclick="ajaxFileUploadForType()"class="easyui-linkbutton" icon="icon-ok" >提交</a> <a class="easyui-linkbutton" icon="icon-cancel" href="javascript:$ht.win.close('uploadTvType')">取消</a> </div> </div><script type="text/Javascript">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("提示信息","選擇的文件必須是EXCEL文件,請確認!"); }else{ $.ajaxFileUpload ( { url: 'uploadTvType', //用于文件上傳的服務器端請求地址 secureuri: false, //是否需要安全協議,一般設置為false fileElementId: 'file', //文件上傳域的ID dataType: 'text', //返回值類型 一般設置為json success: function (responseJSON) //服務器成功響應處理函數 { if(responseJSON == "0"){ $.messager.alert('提示信息','電視臺類型導入成功!'); $ht.win.close('uploadTvType'); $ht.grid.reload('gridTvType'); }else if(responseJSON == "2"){ $.messager.alert('提示信息','沒有符合要求的數據或要導入的數據,在數據庫中已經存在,請確認!'); }else if(responseJSON == "error"){ $.messager.alert('提示信息','電視臺類型導入失敗!'); }else { $.messager.alert('提示信息',responseJSON); $ht.grid.reload('gridTvType'); } }, error: function (data, status, e)//服務器響應失敗處理函數 { $.messager.alert('提示信息','電視臺類型導入失敗!'); } } ) } }else{ $.messager.alert("提示信息","請選EXCEL文件!"); }}</script>
后端代碼:
注:實體類對應Excel里列名稱,可以設置非空校驗(如果為空,這默認該行數據獲取不到),重寫父方法即可,以下代碼注釋重寫了
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 = -5217032731630006972L; @FieldNote(logicalName = "終端類型",physicalName = "terminalType",notNull = true) private String terminalType; @FieldNote(logicalName = "分類編碼",physicalName = "tvTypeCode",notNull = true) private String tvTypeCode; @FieldNote(logicalName = "分類名稱",physicalName = "tvTypeName",notNull = true) private String tvTypeName; @FieldNote(logicalName = "分類縮寫名稱",physicalName = "tvTypeAbridgeName",notNull = true) private String tvTypeAbridgeName; @FieldNote(logicalName = "排序字段",physicalName = "orderBy") private String orderBy; @Override public List<Object> check(BaseModel model, List<Object> valueList){ // valueList = super.check(model,valueList); //TODO:實現非空時的數據檢驗 return valueList; } /** * @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 classChannelInfoManageController{
@RequestMapping("/uploadTvType") public void uploadTvType(HttpServletRequest request, @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception { //該對象為jar中工具類 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++){
TvTypeBeanbean = (TvTypeBean)valueList.get(i);
//輸出字段數據,并插入到相應的表中,省略
System.out.println(bean.getTvTypeCode()+。。。。。。。);
}
}
} }}
新聞熱點
疑難解答