业务:动态生成模板导出Excel,用户修改完再导入Excel.
Spring boot + bootstrap + poi
1.添加Dependence
org.apache.poi poi 3.17 org.apache.poi poi-ooxml 3.17
前者用于引入HSSFWorkbook;后者用于引入XSSFWorkbook.
2.生成模板并导出Excel
//生成模板 function modleCreate(){ //清空数据 var oaId= $("#adjOAId").val(); var adjOrg= $("#adjOrg").val(); var adjDepart=$("#adjDepart").val(); var adjSubject=$("#adjSubject").val(); window.location.href="budgetOaController/exportTemplate?adjOrg="+ adjOrg +"&&adjDepart=" +adjDepart +"&&adjSubject="+adjSubject+"&&oaId="+oaId; }
@RequestMapping(value="/exportTemplate",method=RequestMethod.GET) public void exportTemplate(HttpServletResponse response,@RequestParam String adjOrg,@RequestParam String adjSubject, @RequestParam String adjDepart,@RequestParam String oaId) throws IOException{ // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个Excel表单,参数为sheet的名字 HSSFSheet sheet = workbook.createSheet("模板表"); //创建表头 setTitle(workbook, sheet); List
2.1 导出结果
3.导入Excel
OA预算调整模板导入 导入结果反馈
// 导入Excel @RequestMapping(value = "upload", method = RequestMethod.POST) public AjaxResultJson upload(MultipartFile file) { AjaxResultJson result = new AjaxResultJson(); if (file == null) { result.setMsg("file不能为空"); result.setObj("Excel导入失败"); return result; } Listlist = new ArrayList<>();
String fileName = file.getOriginalFilename(); //获取文件名 try { HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(file.getInputStream())); // 有多少个sheet int sheets = workbook.getNumberOfSheets(); for (int i = 0; i < sheets; i++) { HSSFSheet sheet = workbook.getSheetAt(i); // 获取多少行 int rows = sheet.getPhysicalNumberOfRows(); Budgetadjust budgetadjust = null; // 遍历每一行,注意:第 0 行为标题 for (int j = 1; j < rows; j++) { budgetadjust = new Budgetadjust(); // 获得第 j 行 HSSFRow row = sheet.getRow(j); budgetadjust.setAdjustType(row.getCell(0).toString());// 调整类型 budgetadjust.setApplyDate(row.getCell(1).toString());// 申请日期 budgetadjust.setProcessCode(row.getCell(2).toString());// OA流程编号 budgetadjust.setApplyOrganization(row.getCell(3).toString());// 申请组织 budgetadjust.setApplyDepartment(row.getCell(4).toString());// 申请部门 budgetadjust.setFlag(row.getCell(5).toString());// 是否涉及人力成本 budgetadjust.setExportorganization(row.getCell(6).toString());// 调出组织 budgetadjust.setExportdepartment(row.getCell(7).toString());// 调出部门 budgetadjust.setExportsubject(row.getCell(8).toString());// 调出科目 budgetadjust.setExportmonth(row.getCell(9).toString());// 调出月份 budgetadjust.setExportmoney(row.getCell(10).toString());// 调出金额 budgetadjust.setCostControl(row.getCell(11).toString());// 查询费控系统 budgetadjust.setImportorganization(row.getCell(12).toString());// 调入组织 budgetadjust.setImportdepartment(row.getCell(13).toString());// 调入部门 budgetadjust.setImportsubject(row.getCell(14).toString());// 调入科目 budgetadjust.setImportmonth(row.getCell(15).toString());// 调入月份 budgetadjust.setImportmoney(row.getCell(16).toString());// 调入金额 budgetadjust.setAdjustreason(row.getCell(17).toString());// 调整原因 list.add(budgetadjust); } } budgetAdjustService.saveOABudget(list); } catch (IOException e) { result.setSuccess(false); result.setMsg(e.getMessage()); } result.setSuccess(true); result.setMsg("保存成功"); // 可以传文件名给页面 String fileName = file.getOriginalFilename(); //获取文件名 result.setObj(list); return result; }
注意:
1.要想让标签 能够上传多个文件,只需要在
里添加
multiple
或 multiple="multiple"
属性。
2. Execl 导出锁定列和隐藏单元的值
1.Execl列的锁定 HSSFCellStyle style = workbook.createCellStyle(); style.setLocked(true);//设置列的锁定状态为锁定
2.隐藏单元格的值 hssfRow.createCell(9).setCellValue("12月"); // 调出月
hssfRow.createCell(20).setCellValue("A1"); // 调出期间A1
sheet.setColumnHidden((short)20, true); //隐藏第单元格20的值A1
3.设置表头字体样式背景颜色
private void setTitle(HSSFWorkbook workbook, HSSFSheet sheet) { HSSFRow row = sheet.createRow(0); // 设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度 sheet.setColumnWidth(8, 60 * 256); // 设置为居中加粗 HSSFCellStyle style = workbook.createCellStyle(); HSSFCellStyle style2 = workbook.createCellStyle(); style.setLocked(true);//设置列的锁定状态 style2.setLocked(true); HSSFFont font = workbook.createFont(); HSSFFont font2 = workbook.createFont(); font.setBold(true);//设置字体锁定状态 style.setFont(font); style.setAlignment(HorizontalAlignment.CENTER);//水平居中 style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 // 导出的Excel头部 String[] headers = { "调整类型", "申请日期", "OA流程编号", "申请组织", "申请部门", "是否涉及人力成本", "调出组织", "调出部门", "调出科目", "调出月份", "调出金额", "查询费控系统", "调入组织", "调入部门", "调入科目", "调入月份", "调入金额", "调整原因" }; // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 16); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); if(i==10 || i>11) { font2.setBold(true); font2.setColor(HSSFColor.RED.index); //颜色 style2.setFont(font2); style2.setAlignment(HorizontalAlignment.CENTER);//水平居中 style2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 style2.setFillPattern(FillPatternType.SOLID_FOREGROUND); style2.setFillForegroundColor(IndexedColors.YELLOW.index); row.getCell(i).setCellStyle(style2); }else { cell.setCellStyle(style); } } }
效果图:
----------------------------------------------------
相关链接:https://blog.csdn.net/daihuimaozideren/article/details/78777656
https://www.cnblogs.com/zhuwenxia/p/9443742.html
文件上传链接: https://blog.csdn.net/chenxueshanBlog/article/details/78894838
POI 3.17版本生成excel的一些样式设置: https://blog.csdn.net/m0_37353769/article/details/81872152