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进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

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.