jquery.form.js 利用ajaxSubmit ajax上传Excel,

2024-06-09 08:38

本文主要是介绍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,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1044694

相关文章

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,