***************html代码***************** <form> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',toggle:true" onclick="$('#uploadDiv').window('open')">批量导入</a> </form> <div id="uploadDiv" class="easyui-window" title="导入EXCEL" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:400px;height:200px;padding:10px;"> <div style="font-size:larger;font-weight:200;color:red">请把所有单元格设置为文本格式</div> <div style="text-align:right;padding:5px"> <a href="#" onclick="downLoadTemp();" ><span style="font-size:larger;font-weight:200;color:red">模板下载</span></a> </div> <hr/> <form id="myFormId" action="../njxx/importData" method="post"enctype="multipart/form-data" target="_self"> <div title="Excel导入操作" style="margin-bottom:20px"> <input type="file" id="filePath" name="filePath" data-options="prompt:'请选择文件...'" style="width:100%"> </div> </form> <div style="text-align:right;padding:5px"> <input type="button" value="上传" onclick="mySubmit()"/> </div> </div> ************js代码**************** <script type="text/javascript"> $(function(){ $("#myFormId").form({ success:function(res){ top.$.messager.progress("close"); var resJson = $.parseJSON(res); $('#uploadDiv').window('close'); if(resJson.rtnResult == "success"){ $.messager.alert("提示",resJson.rtnMsg,'info'); $("#recordList").datagrid("reload"); }else{ $.messager.alert("提示", resJson.rtnMsg, 'warning'); } }, error:function(res){ $.messager.alert("提示", "上传失败,请检查网络情况!",'error'); top.$.messager.progress("close"); } }); }); function downLoadTemp(){ window.location.href = app_path + "/njxx/dowmLoadTemp"; } function mySubmit() { top.$.messager.progress({"title":"导入数据","text":"正在导入...","interval":3000}); $("#myFormId").submit(); } </script> ********************后台代码*********************** /** * 导入数据 */ @Before(Tx.class) public void importData() { try { UploadFile file = getFile(); if (file == null) { setAttr("title", "不能导入!"); setAttr("message", "请选择一个要导入的excel文件!"); render("njxxList.jsp"); return; } String filename = file.getFileName(); String uploadPath = file.getUploadPath(); int index = filename.lastIndexOf("."); if (!".xls".equals(filename.substring(index))) { setAttr("title", "不能导入!"); setAttr("message", "请选择execl类型文件!"); render("njxxList.jsp"); return; } HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(new File(uploadPath + "/" + filename))); HSSFSheet sheet = hwb.getSheetAt(0); String msg = this.saveList(sheet); this.sendMsg(msg); } catch (Exception e) { log.error(e.getMessage(), e); this.sendMsg("文件上传失败:"+e.getMessage(), 200); } } /** * 导入Excel的数据,更新或保存 * @param sheet * @return */ public String saveList(HSSFSheet sheet) { long start = System.currentTimeMillis(); // 里面有多少行 int rows = sheet.getLastRowNum(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); int u = 0; int a = 0; List<Record> list = new ArrayList<Record>(); for (int i = 1; i <= rows; i++) {// 从1开始是去除首行-头 HSSFRow row = sheet.getRow(i);// 行数 String jgmc = row.getCell(1).getStringCellValue();// 某行2列 String estdate = row.getCell(2).getStringCellValue();// 某行3列 String regno = row.getCell(3).getStringCellValue();// 某行4列 String lastReportYear = row.getCell(4).getStringCellValue();// 某行5列 String latestReportTime = row.getCell(5).getStringCellValue();// 某行6列 String isException = row.getCell(6).getStringCellValue();// 某行7列 if (jgmc == null || "".equals(jgmc)) { continue; } if (estdate == null || "".equals(estdate)) { continue; } if (regno == null || "".equals(regno)) { continue; } if (lastReportYear == null || "".equals(lastReportYear)) { continue; } if (latestReportTime == null || "".equals(latestReportTime)) { continue; } if (isException == null || "".equals(isException)) { continue; } Record record = new Record(); record.set("jgmc", jgmc); record.set("estdate", estdate); record.set("regno", regno); record.set("lastReportYear", lastReportYear); try { sdf.parse(latestReportTime); record.set("latestReportTime", latestReportTime); } catch (Exception e) { throw new BusinessException("第"+i+"行,最近年报时间格式不正确,正确格式yyyyMMddHHmmss,Excel中的值为:"+latestReportTime); } record.set("isException", "是".equals(isException)?"1":"0"); list.add(record); } UtilLog4j.debug("读取到Excel数据"+list.size()+"行,读取耗时(ms):"+(System.currentTimeMillis()-start)); for (Record record : list) { a++; Db.save(gthTablePreName + "T_GT_2017NJ", record); } UtilLog4j.debug("当前插入"+a+"行,累计耗时(ms):"+(System.currentTimeMillis()-start)); String sql = "DELETE FROM "+gthTablePreName+"T_GT_2017NJ" + " WHERE EXISTS (SELECT T.ID" + " FROM "+gthTablePreName+"T_GT_2017NJ t" + " WHERE "+gthTablePreName+"T_GT_2017NJ.REGNO = T.REGNO" + " AND ("+gthTablePreName+"T_GT_2017NJ.LASTREPORTYEAR < T.LASTREPORTYEAR" + " OR ("+gthTablePreName+"T_GT_2017NJ.LASTREPORTYEAR = T.LASTREPORTYEAR AND "+gthTablePreName+"T_GT_2017NJ.ID < T.ID)))"; int delete = Db.update(sql); UtilLog4j.debug("删除重复数据"+delete+"行,累计耗时(ms):"+(System.currentTimeMillis()-start)); int bad = this.updateBadNjqx(); UtilLog4j.debug("修改为过期的"+bad+"行,累计耗时(ms):"+(System.currentTimeMillis()-start)); int normal = this.updateBadNjqx(); UtilLog4j.debug("修改为未过期的"+normal+"行,累计耗时(ms):"+(System.currentTimeMillis()-start)); return "当前插入"+a+"条,<br/>删除重复数据"+delete+"条,<br/>将"+bad+"条个体户基本信息设置为已过期,<br/>将"+normal+"条个体户基本信息设置为未过期<br/>耗时(ms):"+(System.currentTimeMillis()-start); }