@GetMapping("/template") public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String parameter = req.getParameter("arg"); // if ("chfpi".equals(parameter)) { HSSFWorkbook workbook = null; String fileName = "chfp报考管理模板"+ ".xls"; // Excel文件名 OutputStream os = null; workbook = buildExcelDocument(); resp.reset();// 清空输出流 // web浏览通过MIME类型判断文件是excel类型 resp.setHeader("Content-type", "application/vnd.ms-excel;charset=UTF-8"); // 对文件名进行处理。防止文件名乱码 // Content-disposition属性设置成以附件方式进行下载 resp.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1")); os = resp.getOutputStream();// 取得输出流 workbook.write(os); // } } public HSSFWorkbook buildExcelDocument() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 12); // 设置字体的大小 font.setFontName("宋体"); // 设置字体的样式,如:宋体、微软雅黑等 font.setItalic(false); // 斜体true为斜体 HSSFCellStyle style = workbook.createCellStyle(); Sheet sheetHome = workbook.createSheet("首页"); // 案例展示2行 String[][] exampleStr = new String[][] { { "eg:", "姓名", "手机号码", "考试批", "考试次", "金额" }, { "", "张三", "185xxxxxxxx", "18年6月", "补考一批", "200" }, { "", "李四", "185xxxxxxxx", "18年6月", "补考一批", "200" } }; font.setBold(false); font.setColor(HSSFColor.BLACK.index); style.setFont(font); for (int i = 0; i < exampleStr.length; i++) { Row r = sheetHome.createRow(i); String[] strings = exampleStr[i]; for (int j = 0; j < strings.length; j++) { Cell c = r.createCell(j, Cell.CELL_TYPE_STRING); c.setCellStyle(style); c.setCellValue(strings[j]); } } // 提示信息 HSSFFont fontRemind = workbook.createFont(); fontRemind.setFontHeightInPoints((short) 12); // 设置字体的大小 fontRemind.setFontName("宋体"); // 设置字体的样式,如:宋体、微软雅黑等 fontRemind.setItalic(false); // 斜体true为斜体 HSSFCellStyle styleRemind = workbook.createCellStyle(); fontRemind.setBold(false); // 对文中进行加粗 fontRemind.setColor(HSSFColor.RED.index); // 设置字体的颜色 styleRemind.setFont(fontRemind); Row rowHead = sheetHome.createRow(3); Cell cellHead = rowHead.createCell(0, Cell.CELL_TYPE_STRING); cellHead.setCellStyle(styleRemind); cellHead.setCellValue("姓名、手机号、考试批、考试次、金额为必填项,不能为空!\r\n考试批格式为xx年xx月"); rowHead = sheetHome.createRow(4); cellHead = rowHead.createCell(0, Cell.CELL_TYPE_STRING); cellHead.setCellStyle(styleRemind); cellHead.setCellValue("本页为案列展示页,请跳转至第二页进行操作!"); /*********************************** Sheet第二页 ********************************************/ Sheet sheetInput = workbook.createSheet("客户信息录入"); // HSSFDataValidation setGenderValid = ExcelUtils.setGenderValid(); Row rowInput = sheetInput.createRow(0); String[] title = new String[] { "姓名", "手机号码", "考试批", "考试次", "金额" }; for (int i = 0; i < title.length; i++) { Cell c = rowInput.createCell(i, Cell.CELL_TYPE_STRING); c.setCellStyle(style); c.setCellValue(title[i]); } // sheetInput.addValidationData(setGenderValid); return workbook; }
execl代码导出模板
最新推荐文章于 2023-01-11 10:34:14 发布