日期:2014-05-16 浏览次数:20329 次
1、点击批量导入,先做个弹出层 弹出层里面选择文件和返回信息 确定导入 返回信息,追加到div里面展示
$.getJSON(encodeURI('xxxAction!toImportExcel.action?filePat h='+filePath),function(result){ $.each(result.ERROR_LIST,function(key,value){ $("#message").append("<font color='red'>"+key+":"+value.errorInfor+"</font><br/>"); }); });
ACTION - service 主要调用组件类,封装了map集合,列出错误格式集合或正确导入集合
private JSONObject jsonObject = new JSONObject(); //get-set method JSONObject // 获取 excel path路径 String filePath = new String(ServletActionContext.getRequest().getParameter("filePath").getBytes("iso8859-1"),"utf-8"); // 获取 map 集合 Map<String,List<ExcelBean>> map = this.customerService.toImportExcel(filePath ); //遍历 map Iterator it = map.entrySet().iterator(); List<ExcelBean> errorList = null; List<ExcelBean> successList = null; while(it.hasNext()){ Map.Entry mapEntry = (Map.Entry)it.next(); if(mapEntry.getKey().equals("fail")){ errorList = (List<ExcelBean>)mapEntry.getValue(); }else if(mapEntry.getKey().equals("success")){ successList = (List<ExcelBean>)mapEntry.getValue(); } } // 返回错误集合 或 操作 批量插入 if(errorList.size() != 0){ System.out.println("有错误excel格式,不执行批量操作,返回错误列表"); ServletActionContext.getResponse().setCharacterEncoding("UTF-8"); JSONArray jsonArray = JSONArray.fromObject(errorList); jsonObject.put("ERROR_LIST", jsonArray); ServletActionContext.getResponse().getWriter().write(jsonObject.toSt ring()); System.out.println("json :" + jsonObject.toString()); return null; }else{ // 调用dao层执行批量插入操作 System.out.println("调用dao层执行批量插入操作"); // 封装解析成功的数据格式返回 // dosomething ... return null; }
xml配置
xml 配置层 <!-- cheng ren yin (structs-jquery-json) --> <result-types> <result-type name="json" class="com.opensymphony.webwork.dispatcher.json.JSONResult"></result-type> </result-types> <!-- cry --> <action name="toExportExcel" class="xxxAction" method="toExportExcel"></action> <action name="toImportExcel" class="xxxxAction" method="toImportExcel"> <result name="success" type="json"/> </action>
解析xml组件,把错误的放到 错误集合,正确的放到正确集合
/** * ClassName:ImportExcelUtil * Project: * Company: * * @author 程仁银 * @version * @since Ver 1.1 * @Date 2011 Apr 20, 2011 10:41:21 AM * @see */ public class ImportExcelUtil { private HSSFWorkbook book = null; private HSSFSheet sheet = null; private HSSFRow row = null;// 行对象 public Map<String,List<ExcelBean>> readTemplateFile(String path) { HSSFWorkbook work=null; Map<String,List<ExcelBean>> map = new HashMap<String, List<ExcelBean>>(); List<ExcelBean> listFail = new ArrayList<ExcelBean>(); List<ExcelBean> listSuccess = new ArrayList<ExcelBean>();; try { FileInputStream file = new FileInputStream(path); work=new HSSFWorkbook(file); HSSFSheet sheet=work.getSheetAt(0); HSSFRow row=null;//行对像; HSSFCell cell=null;//列对像; int rowNum ; rowNum=sheet.getLastRowNum();//得到总行数 System.out.println("总行数为"+rowNum); for(int i=1;i<=rowNum;i++) { ExcelBean errorBean = new ExcelBean(); ExcelBean successBean = new ExcelBean(); System.out.println("第 "+i+" 行"); row=sheet.getRow(i); //获取名称 cell=row.getCell((short)0); String name=this.chickCell(cell); System.out.println("名称 = "+ name); if((