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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1