由于是简单记录,所以没有写那么详细,自己先集成好autopoi-web依赖。下面直接贴实现代码:
1、controller层代码
/** * 下载导入模版 * * @param response * @throws IOException */ /* @ApiOperation("下载导入模版") @GetMapping("/downloadTemplate") public void downloadTemplate(HttpServletResponse response) throws IOException { OutputStream out = response.getOutputStream(); ExcelService.downloadTemplate(response, out); }
2、service层代码
/** * 下载导入模版 * @param response * @return */ void downloadTemplate (HttpServletResponse response, OutputStream out);
3、impl层代码
public void downloadTemplate (HttpServletResponse response, OutputStream out) { //将文件输出客户端浏览器 try { //XSSFWorkbook是2007版本,后缀默认是.xlsx XSSFWorkbook workbook = new XSSFWorkbook(); List<String> fieldNameList = getTitle(); String fileName = "【导入模板】上传模版" + DateUtils.formatDateTime(); XSSFSheet sheet = workbook.createSheet("测试"); // sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值 XSSFRow row = null; XSSFCell cell = null; //----------------标题样式--------------------- XSSFCellStyle titleStyle = workbook.createCellStyle();//标题样式 titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); Font titleFont = workbook.createFont(); titleFont.setFontHeightInPoints((short) 22); titleFont.setFontName("微软雅黑"); titleFont.setBold(true); titleStyle.setFont(titleFont); //----------------二级标题样式--------------------- XSSFCellStyle title2Style = workbook.createCellStyle(); Font title2Font = workbook.createFont(); title2Font.setFontName("微软雅黑"); title2Font.setBold(true); //title2Font.setColor(HSSFColor.RED.index); title2Font.setColor((short) 0xa); title2Style.setFont(title2Font); //允许换行 title2Style.setWrapText(true); //----------------列样式1--------------------- XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); Font cellFont = workbook.createFont(); cellFont.setFontName("微软雅黑"); cellFont.setBold(true); //cellFont.setColor(HSSFColor.BLUE.index); //cellFont.setColor((short) 0xc); // 蓝色 cellStyle.setFont(cellFont); //----------------列样式2 必填字段显红色--------------------- XSSFCellStyle cellStyle2 = workbook.createCellStyle(); cellStyle2.setAlignment(HorizontalAlignment.CENTER); cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER); Font cellFont2 = workbook.createFont(); cellFont2.setFontName("微软雅黑"); cellFont2.setBold(true); //cellFont2.setColor(HSSFColor.RED.index); cellFont2.setColor((short) 0xa); // 红色 cellStyle2.setFont(cellFont2); //----------------列样式3 不加粗--------------------- XSSFCellStyle cellStyle3 = workbook.createCellStyle(); cellStyle3.setAlignment(HorizontalAlignment.CENTER); cellStyle3.setVerticalAlignment(VerticalAlignment.CENTER); Font cellFont3 = workbook.createFont(); cellFont3.setFontName("微软雅黑"); cellStyle3.setFont(cellFont3); //------------------- 创建第一行(字段名) -------------------- row = sheet.createRow(0); for (int i = 0; i < fieldNameList.size(); i++) { cell = row.createCell(i); if (i==0 || i==2 || i==4) { // 024行红色必填(这里指定列标为红色) cell.setCellStyle(cellStyle2); } else { cell.setCellStyle(cellStyle); } cell.setCellValue(fieldNameList.get(i)); } // 这里写例子数据 List<Map<String, Object>> exampleDataList = getExampleData(); List<String> contentList = getExampleTitle(); for (int j = 0; j < exampleDataList.size(); j++) { row = sheet.createRow(j + 1); Map<String, Object> map = exampleDataList.get(j); for (int k = 0; k < map.size(); k++) { cell = row.createCell(k); cell.setCellStyle(cellStyle3); cell.setCellValue(map.get(contentList .get(k)).toString()); } } response.setContentType("application/binary;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); FileUtils.setAttachmentResponseHeader(response, fileName); workbook.write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 通过类型获取标题头 * * @return */ public List<String> getTitle() { List<String> fieldNameList = new ArrayList<>(); String str = "姓名"; // 这里是导出模板的表头数据 String[] str = str.split(","); for (int i = 0; i < str.length; i++) { fieldNameList.add(str[i]); } return fieldNameList; }
/** * 这里是初始化一些样例数据(如有需要则从数据库获取,否则就写死) * * @return */ public List<Map<String, Object>> getExampleData() { List<Map<String, Object>> list = new ArrayList<>(); Map<String, Object> map1 = new HashMap<>(); map1.put("name", "张三"); list.add(0, map1); return list; }
/** * 这里写匹配的字段名 * * @return */ public List<String> getExampleTitle() { List<String> list = new ArrayList<>(); list.add("name"); return list; }