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

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T