实现 xls 数据的导入 发表于 2018-08-05 | 分类于 Java 高级 | 阅读次数: 字数统计: 795 字 | 阅读时长 ≈ 4 分钟 实现 xls 数据的导入 html12345678<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="batchAddClient()">导入信息</a><!-- 导入框 --><div id="importCheckWin" class="easyui-dialog" data-options="onBeforeOpen:getDialogCheckUrl,closed:true" style="width:489px; height:268px;"> <iframe frameborder="0" id = "impIframeCheck" width="100%" height="100%" scrolling="no" src=""></iframe> </div> js 方法1234function getDialogCheckUrl(){ var url = getHost(HostAddress.uomp)+'/uomp/cdaBaseDataRemarks/importCheckWin'; $('#impIframeCheck').attr('src',url);} controller123456789101112131415@Controller@RequestMapping(value = "/cdaBaseDataRemarks")public class CdaBaseDataRemarksController extends BaseController { /** * 打开批量检查导入窗口 * @param locale * @param model * @return */ @RequestMapping(value = "/importCheckWin", method = RequestMethod.GET) public String importCheckWin(Locale locale, Model model) { model.addAttribute("uihost", SysConstant.config.getProperty("uihost")); return "importBatchRemarksCheck"; }} jsp 页面123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>批量检查客户窗口</title><script type="text/javascript" src="${uihost}/sunriseui/sunriseui.js"></script><script type="text/javascript"> load('ajaxfileupload')</script><script type="text/javascript"> var map;var index=0; var mapLenght=0;/*map长度 */ var successNum=0;/*成功条数*/ var failNum='';/*错误行号*/ //存在跨域问题 不能够获取到返回值 -- 批量检查提交 function coreSelect() { $('#tipgrid').empty(); $.ajaxFileUpload({ url : getHost(HostAddress.uomp) + '/uomp/cdaBaseDataRemarks/importBatchClientCheckData', secureuri : false, // 是否需要安全协议,一般设置为false id : "filesform", fileElementId : 'files', // 文件上传域的ID dataType : 'json', // 返回值类型 一般设置为json data : { }, async : false, success : function(data){// 服务器成功响应处理函数 if(data.returnCode==ReturnCode.success){ $('#importRotaff').form('reset'); $('#tipgrid').datagrid({ data:data, title:'导入结果', fitColumns:false, border:false, columns:[[ {field:'relustMsg',title:'备注信息导入结果',width:500} ]] }); $('#tipgrid').append("<span>"+data.returnMessage+'</span>'); }else{ $.messager.alert('警告', data.returnMessage,'warning'); } } }); } function fileChange(target){ var fileSize = 0; if(!target.files){ var filePath = target.value; var fileSystem = new ActiveXObject("Scriting.FileSystemObject"); var file = fileSystem.GetFile(filePath); fileSize = file.Size; }else{ fileSize = target.files[0].size; } var size = fileSize/1024; if(size>3000){ $.messager.alert('警告','导入数据超过3M,请分批导入!','warning'); $('#subBtn').attr('disable'); return; }else{ $('#subBtn').linkbutton('enable'); } }</script></head><body class="easyui-layout" data-options="fit:true"> <div data-options="region:'center'" style="background: #eee;"> <form id="importRotaff" action="" enctype="multipart/form-data"> <table style="width: 100%"> <tr> <td colspan="2"> <input type="file" name="files" onchange="fileChange(this)" id="files" style="width: 200px"> <a id="subBtn" href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="coreSelect()">导入信息</a> </td> </tr> <tr> <td align="center" id="thistd"></td> </tr> </table> </form> <div id="tipgrid"></div> </div></body></html> 数据导入 controller1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859@Controller@RequestMapping(value = "/cdaBaseDataRemarks")public class CdaBaseDataRemarksController extends BaseController { /** * 读取批量检查Excel导入 * @param locale * @param model * @return */ @RequestMapping(value = "/importBatchClientCheckData", method = RequestMethod.POST) @ResponseBody public ResultBean<LogImportBean> importBatchClientCheckData(Locale locale, @RequestParam MultipartFile[] files,@ModelAttribute("CdaBaseDataRemarks") CdaBaseDataRemarks bean) throws Exception{ ResultBean<LogImportBean> rb = new ResultBean<LogImportBean>(); //返回导入结果明细信息列表 List<LogImportBean> logs = new ArrayList<LogImportBean>(); rb.setRows(logs); if(files[0].getSize()==0){ rb.setReturnCode(SysConstant.SYS_RETURN_FAILED_CODE); rb.setReturnMessage("未选择文件,请选择您要导入的execl文件"); return rb; } InputStream is = files[0].getInputStream(); Workbook book = Workbook.getWorkbook(is); Sheet sheet = book.getSheet(0); int rownum = sheet.getRows(); // 解析到表格中的数量,判断是否超过导入个数限制 if (rownum > 1) { rb.setReturnCode(SysConstant.SYS_RETURN_SUCCESS_CODE); rb.setReturnMessage(SysConstant.SYS_RETURN_SUCCESS_MESSAGE); for (int i = 1; i < rownum; i++) { //第一个参数是列,第二个参数是行 String custId = sheet.getCell(0, i).getContents(); String certNo = sheet.getCell(1, i).getContents(); String certType = sheet.getCell(2, i).getContents(); String remarks = sheet.getCell(3, i).getContents(); // 判断字符串是否在大于20 if(remarks.length()>20){ logs.add(new LogImportBean("备注内容不能大于20个字")); } // 新增自己的业务代码 }// for 循环结束 }else{ rb.setReturnCode(SysConstant.SYS_RETURN_FAILED_CODE); rb.setReturnMessage("表格不能为空!"); } return rb; }} 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 微信支付 支付宝 本文作者: ShenWenFang 本文链接: http://swenfang.github.io/2018/08/05/数据导入/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处! 推荐文章 文件的上传和下载 图片上传base64 webstorm 配置 Apache 服务器 你的面试题