struts2+ajaxSubmit+poi导入excel
来源:互联网 发布:高清网络电视在线观看 编辑:程序博客网 时间:2024/05/22 12:32
form表单
<form id="upLoadForm" style="margin-bottom:3"><input type="hidden" name="hasGrupRole" value="<s:property value='hasGrupRole'/>"> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce" onkeypress="enterQuery()"> <tr> <td class="table_head_td" align="center">上传:</td> <td class="table_head_td" ><input id="excelFile" name="files" type="file"/></td> </tr> <tr> <td class="table_head_td" align="center" colspan="2"> <input type="button" value="提交" accesskey="M" title="ALT+M" class="btn0" onclick="submitExcel();"/> </td> </tr> </table></form>
js
function submitExcel(){if(confirm('确定上传数据!')){ var excelFile = $("#excelFile").val(); if(excelFile=='') { alert("请选择需上传的文件!"); return false; } if(excelFile.indexOf('.xls')==-1){ alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)!"); return false; }$("#upLoadForm").ajaxSubmit({ type: "POST", encType:'multipart/form-data', url: '提交路径', data:{'files':excelFile// files action定义属性 File类型 }, success: function(data){ alert(data);//返回值});return false;//如不返回false会跳转页面}}action
public void doUpload(){try{try {this.service.doUpLoad(model);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}finally{//Struts2Utils.getResponse().setContentType("text/xml;charset=utf-8");//返回值报错Struts2Utils.getResponse().setHeader("Content-type", "text/html;charset=UTF-8");//如不设置头信息会中文会出现火星文类型乱码Struts2Utils.getResponse().setCharacterEncoding("UTF-8"); //如不设置编码类型中文会出现‘?????’乱码try {Struts2Utils.getResponse().getWriter().print(this.getModel().getMessage());//打印返回值传至前端} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
public void doUpLoad(TransCenterModel model) throws Exception{ // 检查后缀名是否符合条件,同时更改上传文件的文件名 int filesize = model.getFilesFileName().length(); String fileEx = model.getFilesFileName().substring( model.getFilesFileName().indexOf("."), filesize); //获取文件名 // String fileName=model.getFilesFileName().substring(0,model.getFilesFileName().indexOf(".")); // 获得上传路径 String realPath = ServletActionContext.getServletContext().getRealPath( "/UploadFile/"); File saveFile=null; ArrayList<ArrayList<String>> row=null; if (model.getFiles() != null) { saveFile = new File(new File(realPath), model.getFilesFileName()); if (!saveFile.getParentFile().exists()) { saveFile.getParentFile().mkdirs(); } FileUtils.copyFile(model.getFiles(), saveFile);// 到这里,文件已上传成功 if (fileEx.equals(".xls") || fileEx.equals(".xlsx")) { String files=saveFile.getPath(); String excel=files.substring(files.lastIndexOf("\\")).substring(1); files=files.substring(0, files.lastIndexOf("\\")); files=files.replaceAll("\\\\","\\\\\\\\"); row=readExcel(excel,files); }<pre name="code" class="java"><span></span> <span>for (ArrayList<String> cell : row) { <span></span>/</span>/数据处理
<span></span> }
}
this.addMessages(model, "trans.upload.succ",new String[]{});}@SuppressWarnings("deprecation")public ArrayList<ArrayList<String>> readExcel(String fileName,String path) { ArrayList<ArrayList<String>> Row =new ArrayList<ArrayList<String>>(); try { Workbook workBook = null; try { workBook = new XSSFWorkbook(path+"\\"+fileName); } catch (Exception ex) { workBook = new HSSFWorkbook(new FileInputStream(path+"\\"+fileName)); } for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) { Sheet sheet = workBook.getSheetAt(numSheet); if (sheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { Row row = sheet.getRow(rowNum); if (row == null) { continue; } // 循环列Cell ArrayList<String> arrCell =new ArrayList<String>(); for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) { Cell cell = row.getCell(cellNum); if (cell == null) { continue; } arrCell.add(getValue(cell)); } Row.add(arrCell); } } } catch (IOException e) { System.out.println("e:"+e); } return Row; } private String getValue(Cell cell) { cell.setCellType(Cell.CELL_TYPE_STRING); return cell.toString(); }
0 0
- struts2+ajaxSubmit+poi导入excel
- 基于Spring +Apache POI 导入 ajaxSubmit提交方式导入excel
- Struts2.18 Poi-3.7 Excel 导入导出
- Struts2.18 Poi-3.7 Excel 导入导出 .
- Struts2.18 Poi-3.7 Excel 导入导出 .
- Struts2.18 Poi-3.7 Excel 导入导出
- struts2中使用poi导入excel数据
- struts2将excel文件内容导入数据库 ----- POI
- struts2结合poi-3.7实现excel文件数据导入
- poi 导入excel源码
- POI导入excel出错
- 使用POI 导入excel
- POI导入Excel
- excel导入(poi)
- poi导入导出excel
- poi excel导入导出
- poi 导入 Excel
- POI实现excel导入
- 【路径大全】iphone所有文件路径。CYDIA
- Android网络操作和优化相关
- HDU 2602 -Bone Collector
- NodeJS 和 ThinkJS 使用时的注意点 (一)
- 缓存更新的套路
- struts2+ajaxSubmit+poi导入excel
- 循环队列
- 用jQuery实现焦点图轮播(底部是小圆点)
- Android模拟器使用PC端USB转串口调试
- HTTP 协议详解
- C++中Static用法
- sprintf与snprintf
- 嵌入式系统C编程之堆栈回溯
- <tr> 外面直接用<div> 控制是否显示行 无效