1.导入功能思路:
前台上传文件将MultipartFile
-》输入流
-》poi work对象
-》使用excel表头与model字段对应关系
-》通过sheet、row、cell(相关校验)转换成modelList
-》(相关校验)对应的业务处理。
2.导入前台vue的axios请求需要content-Type设置:
content-Type :multipart/form-data
需要fromData对象append("key",value)进行传文件和传参,
传对象类型参数就需要用到Json.stringify和blob。
3.导入后台接收形式:
@RequestParam(value="key")和@RequestPart(value="key")接收。
@RequestParam适用于基本类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML)。
4.导出:
content-Type :application/vnd.ms-excel
5.java-poi导出导入excel
1.导出: a.创建HSSFWorkbook,对应Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(); b.创建sheet HSSFSheet sheet = workbook.createSheet("sheetName"); c.在sheet中添加表头,第0行就是表头 HSSFRow row = sheet.createRow(0); d.设置单元格格式,居中 HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); e.填充表头单元格内容 HSSFCell headCell = hssfRow.createCell(0); headCell.setCellValue("A"); headCell.setCellStyle(cellStyle); HSSFCell headCell = hssfRow.createCell(1); headCell.setCellValue("B"); headCell.setCellStyle(cellStyle); f.添加数据内容 for(int i = 1,i<modelList.length,i++){ hssfRow = sheet.createRow(i); cell.setCellValue(modelList.getName()); cell.setCellStyle(cellStyle); } g.通过流导出 workbook.write(outputStream); 2.导入: a.上传文件 b.得到excel if (fileName.endsWith("xlsx")) { hssfWorkbook = new XSSFWorkbook(is);// Excel 2007 } else if (fileName.endsWith("xls")) { hssfWorkbook = new HSSFWorkbook(is);// Excel 2003 } c.循环sheet循环row单元格数据读取到modelList d.具体业务处理
参考链接:https://www.cnblogs.com/fighter007/p/10917026.html
参考链接:https://blog.csdn.net/yh869585771/article/details/80191786
参考链接:https://www.cnblogs.com/sharpest/p/11557024.html
原文:https://www.cnblogs.com/duiyuedangge/p/14418493.html