excel导入导出与通过poi处理数据
导出excel文件
需要用到webUpLoader插件
- controller端代码
1 @RequiresPermissions("financial:financialList:list") 2 @RequestMapping("export") 3 public String exportFile(WorkOrder workOrder, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) throws Exception { 4 try { 5 String fileName = "BankStatement" + DateUtils.getDate("yyyyMMddHHmmss")+".xlsx"; 6 List<TestingRule> list = new ArrayList<>(); 7 new ExportExcel("模版", 实体类.class).setDataList(list).write(response, fileName).dispose(); 8 } catch (Exception e) { 9 e.printStackTrace(); 10 response.getWriter().write("<script>top.layer.alert('导出失败!', {icon: 0, title:'提示'})</script>"); 11 } 12 return null; 13 }
- html端代码
1 <button type="button" id="export-btn" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="left" title="导出Excel"> 2 <i class="glyphicon glyphicon-cloud-upload"></i>导出Excel 3 </button> 4 <script> 5 $(function(){ 6 $("#export-btn").on("click", function () { 7 location.href = "${ctx}/system/sys/testingRule/export"; 8 }); 9 }); 10 </script>
导入excel读取内容
- controller端代码
1 //上传模版 2 @RequestMapping("upload") 3 @ResponseBody 4 public AjaxJson upload(@RequestParam MultipartFile file) { 5 try { 6 //将MultiparFile文件转换成InputStream 7 CommonsMultipartFile cf = (CommonsMultipartFile) file; 8 InputStream inputStream = cf.getInputStream(); 9 //获取excel工作簿 10 Workbook workbook = new HSSFWorkbook(inputStream); 11 //获取第一个表 12 Sheet sheet=workbook.getSheetAt(0); 13 //获取行数 14 int rows = sheet.getLastRowNum(); 15 List<String> list = new ArrayList<>(); 16 //遍历所有行 17 for(int i = 0; i<=rows ; i++) { 18 //获取第i行第一个单元格内容并自动转为String类型 19 list.add(this.getStringValueFromCell(sheet.getRow(i).getCell(0))); 20 } 21 22 AjaxJson json = getSuccessAjaxJson("上传成功!"); 23 json.getBody().put("uploadId", "ss"); 24 return json; 25 } catch (Exception e) { 26 e.printStackTrace(); 27 return getErrorAjaxJson("1", "上传失败!"); 28 } 29 }
- 获取excel 文件内容自动更改为String
1 public static String getStringValueFromCell(Cell cell) { 2 SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy"); 3 DecimalFormat decimalFormat = new DecimalFormat("#.#"); 4 String cellValue = ""; 5 if(cell == null) { 6 return cellValue; 7 } 8 else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { 9 cellValue = cell.getStringCellValue(); 10 } 11 12 else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { 13 if(HSSFDateUtil.isCellDateFormatted(cell)) { 14 double d = cell.getNumericCellValue(); 15 Date date = HSSFDateUtil.getJavaDate(d); 16 cellValue = sFormat.format(date); 17 } 18 else { 19 cellValue = decimalFormat.format((cell.getNumericCellValue())); 20 } 21 } 22 else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { 23 cellValue = ""; 24 } 25 else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { 26 cellValue = String.valueOf(cell.getBooleanCellValue()); 27 } 28 else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) { 29 cellValue = ""; 30 } 31 else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { 32 cellValue = cell.getCellFormula().toString(); 33 } 34 return cellValue; 35 }
- 导入html
1 $(function (){ 2 // 初始化 WebUploader 3 var uploader = WebUploader.create({ 4 auto: true, 5 swf: '${ctxStatic}/webuploader-0.1.5/Uploader.swf', 6 server: '${ctx}/system/sys/testingRule/upload', 7 resize: false, 8 pick: '#filePicker', //绑定div转为按钮 9 duplicate: true, 10 fileNumLimit: 1 11 }); 12 $("#qwe").click(function(){ 13 $("#filePicker").find("input:file").trigger("click"); 14 }) 15 uploader.on( 'uploadSuccess', function( file ) { 16 layer.alert("导入成功!"); 17 location.reload(); 18 }); 19 uploader.on( 'uploadError', function( file ) { 20 layer.alert("导入出错!"); 21 }); 22 }); 23 24 <div id="filePicker" class="hidden"></div> 25 <button id="qwe" type="button" class="btn btn-primary btn-sm"> 26 <i class="glyphicon glyphicon-cloud-upload"></i> 上传 27 </button>

浙公网安备 33010602011771号