表单/ajax上传excel文件
来源:互联网 发布:hibernate源码分析 编辑:程序博客网 时间:2024/06/07 03:48
form表单提交:
<form id="upload-form" method="post" action="/import" enctype="multipart/form-data"> <div> <input type="file" name="excelfile" id="excelfile"/> <input type="hidden" name="fileType" value="10"/> </div> <input type="submit" id="upload" value="上传"/></form>
ajax异步上传:
<form id="upload-form" method="post" action="/import" enctype="multipart/form-data"> <input type="file" name="excelfile" id="excelfile" value="" /> <input type="hidden" name="fileType" value="10"> <input type="button" id="upload" value="上传"/></form>js: $("#upload").click(function(){ var formdata = new FormData(document.getElementById("upload-form")); $.ajax({ url:"/import", type:"post", data:formdata, contentType:false, //- 必须false才会自动加上正确的Content-Type processData: false, //- 必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理 success:function(){ alert("ok"); }, error:function(){ alert("fail"); } }); });
后台代码:
@RequestMapping(value = "/import") public ModelAndView importExcel(@RequestParam(value = "excelfile") MultipartFile file, String fileType, HttpServletRequest request){ try { List<Map> list = readExcelFile(file); } catch (Exception e) { e.printStackTrace(); } ModelAndView mv = new ModelAndView("search"); return mv; } private List<Map> readExcelFile(MultipartFile file) throws Exception { String fileName = file.getOriginalFilename();//获取文件名 long size = file.getSize(); String format = fileName.substring(fileName.lastIndexOf(".")+1); System.out.println("文件名:"+fileName+",大小:"+size/1024+"kb,格式:"+format); Workbook workbook = null; InputStream inputStream = file.getInputStream(); //- excel 2003 if("xls".equals(format)){ workbook = new HSSFWorkbook(inputStream); }else if ("xlsx".equals(format)){ workbook = new XSSFWorkbook(inputStream); }else { throw new Exception("文件格式非法!"); } /** 读取第一个sheet */ List<Map> result = new ArrayList<>(); int sheets = workbook.getNumberOfSheets(); if(sheets > 0){ Sheet sheet = workbook.getSheetAt(0); result = readSheet(sheet); } inputStream.close(); return result; } /** * 解析sheet. * sheet前三列分别为对应到bean的字段、字段类型、excel表头 * * @param sheet * @return */ private List<Map> readSheet(Sheet sheet) { List<Map> result = new ArrayList<Map>(); int totalRows = sheet.getPhysicalNumberOfRows();//- Excel行数 int totalCells = 0;//- Excel列数 String[] fields = null; //- 存储对应bean的字段名称 Row firstRow = sheet.getRow(0); if (totalRows > 0 && firstRow != null) { totalCells = firstRow.getPhysicalNumberOfCells(); fields = new String[totalCells]; for (int c = 0; c < totalCells; c++) { fields[c] = firstRow.getCell(c).getStringCellValue(); } }else return result; /** 循环Excel的行, 第四行是正文开始 */ for (int r = 4; r < totalRows; r++) { Row row = sheet.getRow(r); if (row == null){ continue; } Map map = new HashMap(); /** 循环Excel的列 */ for (int c = 0; c < totalCells; c++) { Cell cell = row.getCell(c); if (null != cell) { //- cell是数值类型 if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ map.put(fields[c], cell.getNumericCellValue()); } //- cell是字符串类型 else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){ map.put(fields[c], cell.getStringCellValue()); } } } result.add(map); } return result; }
处理excel的maven依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> <exclusions> <exclusion> <artifactId>commons-codec</artifactId> <groupId>commons-codec</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency>
阅读全文
0 0
- 表单/ajax上传excel文件
- 文件上传+ajax 实现表单文件上传
- form表单,AJAX上传文件
- Ajax表单提交上传文件
- ajax from表单上传文件
- Ajax提交form表单,Ajax文件上传
- 无刷新文件上传 ajax提交表单
- 无刷新文件上传 ajax提交表单
- 无刷新文件上传 ajax提交表单
- 表单上传文件提交实现ajax方式
- 上传文件(ajax结合form表单)
- 使用ajax提交表单或上传文件
- ajax上传form表单包含文件
- SpringBoot+Ajax文件上传+FormData表单提交
- springmvc Ajax表单上传文件案例
- ajax异步上传文件(无需表单)
- jquery.ajax()上传带文件的表单
- 多文件上传,ajax提交表单
- MyEclipse安装JS代码提示(Spket插件)
- 快速排序法
- 大型网站架构模式
- Java线程:线程的交互
- C++之const限定符
- 表单/ajax上传excel文件
- hibernate笔记-017-中间表含其他字段
- 起点
- 如何查看keras的默认backend
- 1141: 进制转换
- 文件系统学习
- Tomat8安装与配置
- java 设计模式探索
- 从尾到头打印链表