本文主要是介绍jquery.form.js 利用ajaxSubmit ajax上传Excel,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
以下代码只供参考。
需要引入jquery.form.js,
下载地址:http://download.csdn.net/download/u011474272/9974610
html代码
<div class="insurCombination" id="insurCombination" style='display:none;'><div class="warpT"><b>人员导入</b><span class="close" id="close"></span></div><div class="warpM" style='height:auto;'><form id="upload" action="/action/clubs/importClubPersonInfoFromExcel.json" method="post" enctype="multipart/form-data"><table border="0" cellspacing="0" cellpadding="0"><tr height="60"><td width="110" align="right"><em class="myRed" style='font-style:normal;'>名单文件:</td><td width="248"><div style="margin-top:30px"><input type='text' name='textfield' id='textfield' readonly="readonly" class='input-circle' style='height:25px;width: 170px;border: 1px solid #9DCCF7;' /> <input type='button' class="Inus-label Inus-label2" value='浏览...' style=" height: 25px;width: 60px;border: 1px solid #9DCCF7;border-radius: 5px"/><input type="file" name="file" size="28" style="position:absolute;right:80px;filter:alpha(opacity:0);opacity:0;height:28px;width:70px;line-height:28px; cursor:pointer;"οnchange="document.getElementById('textfield').value=this.value"accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /></div></td></tr> </table></form></div><div class="warpB"> <div style="text-align: center;" id="upButton"><a style="display: block; height: 30px;border: 1px solid #9dccf7;line-height: 30px; border-radius: 12px;color: #111111;" href="#">确认上传</a></div></div>
</div>
js代码
$(document).ready(function(){$("#upButton").bind('click', function(){submitExcelFrom(); });
});
//导入
function submitExcelFrom(){var epath = $('#textfield',window.parent.document).val(); if(epath==""){ alert( '导入文件不能为空!'); return; } if(epath.substring(epath.lastIndexOf(".") + 1).toLowerCase()=="xlsx"){ alert( '03以上版本Excel导入暂不支持!'); return; } if (epath.substring(epath.lastIndexOf(".") + 1).toLowerCase()!="xls") { alert( '导入文件类型必须为excel!'); return; } $("#upload").ajaxSubmit({ type: "post", dataType: "json", // 'xml', 'script', or 'json' (expected server response type) url: __webRoot + '/action/clubs/importClubPersonInfoFromExcel.json',success: function (data1) {if(data1.flag){ alert('导入成功'); }else{ alert(data1.opr_msg); } }, error: function (msg) { alert("文件上传失败"); } });
}
Java 代码
@RequestMapping(value="importClubPersonInfoFromExcel.json", method={RequestMethod.POST})@ResponseBodypublic Map<String,Object> importClubPersonInfoFromExcel(@RequestParam("file") MultipartFile file,@RequestParam Map<String, Object> params) throws IOException, Exception {Map<String,Object> result = new HashMap<String, Object>();String opr_msg = "导入成功!";if(file == null || file.isEmpty()) {opr_msg = "未获取文件对象!";result.put("opr_msg", opr_msg);result.put("flag", false);return result;}//>10Mlong maxFileSize = 10<<20;if(file.getSize() > maxFileSize) {opr_msg = "文件大小不能超过10M!";result.put("opr_msg", opr_msg);result.put("flag", false);return result;}String fileName = file.getOriginalFilename();if(fileName.lastIndexOf(".xls") == -1 && fileName.lastIndexOf(".xlsx") == -1) {opr_msg = "文件格式不正确!";result.put("opr_msg", opr_msg);result.put("flag", false);return result;}opr_msg = clubPersoninfoService.importClubPersoninfoExcel(file.getInputStream(),params);if(!"success".equals(opr_msg)){result.put("opr_msg", opr_msg);result.put("flag", false);return result;}result.put("flag", true);return result;}
@Overridepublic String importClubPersoninfoExcel(InputStream is,Map<String,Object> params) throws Exception {/** Excel读取*///默认sheet名称String DEFAULT_SHEETNAME = "俱乐部人员信息导入";Workbook wb = WorkbookFactory.create(is);Sheet sheet = wb.getSheet(DEFAULT_SHEETNAME);//如果不是默认命名,则获取第一项if(sheet == null) {sheet = wb.getSheetAt(0);}if(sheet == null) {return "sheet页不存在!";}Iterator<Row> row_iter = sheet.iterator();Set<ClubPersoninfoBean> set = new HashSet<ClubPersoninfoBean>();if(row_iter.hasNext()) {//Skip first rowrow_iter.next();while(row_iter.hasNext()) {Row r = row_iter.next();//Check valuesString no_cell_title = "序号";Cell no_cell = r.getCell(0);if(no_cell == null) {return "序号为"+no_cell+"的"+no_cell_title+"不可为空!";}no_cell.setCellType(Cell.CELL_TYPE_STRING);if(no_cell.getCellType() != Cell.CELL_TYPE_STRING) {return "序号为"+no_cell+"的"+no_cell_title+"格式不正确!";}String saleCode_cell_title = "销售人员工号";Cell saleCode_cell = r.getCell(1);if(saleCode_cell == null) {return "序号为"+no_cell+"的"+saleCode_cell_title+"不可为空!";}saleCode_cell.setCellType(Cell.CELL_TYPE_STRING);if(saleCode_cell.getCellType() != Cell.CELL_TYPE_STRING) {return "序号为"+no_cell+"的"+saleCode_cell_title+"格式不正确!";}String saleName_cell_title = "销售员姓名";Cell saleName_cell = r.getCell(2);if(saleName_cell == null) {return "序号为"+no_cell+"的"+saleName_cell_title+"不可为空!";}saleName_cell.setCellType(Cell.CELL_TYPE_STRING);if(saleCode_cell.getCellType() != Cell.CELL_TYPE_STRING) {return "序号为"+no_cell+"的"+saleName_cell_title+"格式不正确!";}String remarks_cell_title = "备注";Cell remarks_cell = r.getCell(3);String remarks_cell_str = "";if(remarks_cell == null || "".equals(remarks_cell)) {remarks_cell_str = null;}else{remarks_cell_str = remarks_cell.toString().trim(); remarks_cell.setCellType(Cell.CELL_TYPE_STRING);if(remarks_cell.getCellType() != Cell.CELL_TYPE_STRING) {return "序号为"+no_cell+"的"+remarks_cell_title+"格式不正确!";}}ClubPersoninfoBean clubPersoninfoBean = new ClubPersoninfoBean();clubPersoninfoBean.setSales_no(saleCode_str);clubPersoninfoBean.setSales_name(saleName_str);clubPersoninfoBean.setRemarks(remarks_cell_str);set.add(clubPersoninfoBean);}}List<ClubPersoninfoBean> list = new ArrayList<ClubPersoninfoBean>(set);return insertBatchClubPersoninfo(list);}
代码只做参考。
这篇关于jquery.form.js 利用ajaxSubmit ajax上传Excel,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!