/** * 实现层 * * @param param * @return */ public void downLoadTemplate(DownLoadParam param, HttpServletResponse response) { //获取列名,在数据库查询即可 List<xxxField> columnNameList = getFields(param); //生成模板 getTemplate(columnNameList, response); } /** * 查询模板字段 * * @param param * @return */ @Override public List<xxxField> getFields(DownLoadParam param) { //根据配置判断是否可进行操作 xxxDesc xxxDesc = xxxxService.lambdaQuery() .eq(xxxDesc::getFormNo, param.getFormNo()) .last("limit 1") .one(); if (Objects.isNull(xxxDesc)) { throw new AppException(AppError.FAILED.getCode(), "没有找到下载的模板,请检查!"); } List<xxxField> fieldList = xxxFieldService.lambdaQuery().eq(xxxField::getFormId, xxxDesc.getId()) .orderByAsc(xxxField::getFieldOrder) .list(); return fieldList; } @Override public void getTemplate(List<xxxField> list, HttpServletResponse response) { FileOutputStream out = null; try { // excel对象 HSSFWorkbook workbook = new HSSFWorkbook(); // sheet对象 HSSFSheet sheet = workbook.createSheet("sheet1"); mergeCells(sheet, 0, 1, 0, 40); //设置列宽 for (int i = 0; i < 39; i++) { sheet.setColumnWidth(i, 3000); // 第一列的列宽 } // 创建标题行样式 CellStyle headerStyle = workbook.createCellStyle(); Font headerFont = workbook.createFont(); headerFont.setColor(HSSFColor.RED.index); headerFont.setFontName("SimSun"); headerFont.setCharSet(HSSFFont.DEFAULT_CHARSET); // 设置字符集 headerStyle.setFont(headerFont); headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 创建数据行样式(可选) CellStyle dataStyle = workbook.createCellStyle(); Font dataFont = workbook.createFont(); dataStyle.setFont(dataFont); dataStyle.setAlignment(HorizontalAlignment.CENTER); dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); dataStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 创建标题行 Row headerRow = sheet.createRow(0); Cell headerCell = headerRow.createCell(0); headerRow.setHeightInPoints(63); headerCell.setCellValue("填写须知"); headerCell.setCellStyle(headerStyle); headerCell = headerRow.createCell(1); headerCell.setCellValue("1.带*项为必填项,若不填写,将无法导入\n" + "2.不可改变表格样式"); headerStyle.setWrapText(true); // 设置自动换行 headerCell.setCellStyle(headerStyle); //处理模板列 int i = 0; Row row = sheet.createRow(1); row.setHeightInPoints(20); for (xxxField xxxField : list) { //如果字段是必填,需要拼接红色* if (YesNoEnum.YES.getCode().equals(xxxField.getIsRequired())) { String sign = "*" + xxxField.getColumnName(); setRedValue(workbook, i, row, sign); } else { row.createCell(i).setCellValue(xxxField.getColumnName()); } //下拉选赋值 setSelectValue(sheet, i, selectValueList); //设置单元格样式 row.getCell(i).setCellStyle(dataStyle); // 设置单元格背景色 setCellBackground(row.getCell(i), IndexedColors.YELLOW); // 设置单元格边框样式 setCellBorders(row.getCell(i), BorderStyle.THIN); i++; } OutputStream output; output = response.getOutputStream(); //清空缓存 response.reset(); //定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义) response.setHeader("Content-disposition", "attachment;filename=" + new String("导入模板".getBytes(), "iso-8859-1") + ".xls"); //定义下载的类型,标明是excel文件 response.setContentType("application/vnd.ms-excel"); //这时候把创建好的excel写入到输出流 workbook.write(output); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private void setSelectValue(HSSFSheet sheet, int i, List<String> selectValueList) { DataValidationHelper validationHelper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(selectValueList.toArray(new String[0])); CellRangeAddressList addressList = new CellRangeAddressList(2, 200000, i, i); // 第一行第一列 DataValidation validation = validationHelper.createValidation(constraint, addressList); validation.setSuppressDropDownArrow(false); // 隐藏下拉箭头 validation.setShowErrorBox(true); // 显示错误提示框 sheet.addValidationData(validation); } private static void setRedValue(HSSFWorkbook workbook, int i, Row row, String sign) { // 创建红色字体样式 CellStyle style = workbook.createCellStyle(); Font redFont = workbook.createFont(); redFont.setColor(IndexedColors.RED.getIndex()); redFont.setBold(true); redFont.setFontHeightInPoints((short) 11); // 设置字体大小为11磅 style.setFont(redFont); // 创建富文本字符串 RichTextString richText = workbook.getCreationHelper().createRichTextString(sign); richText.applyFont(0, 1, redFont); // 应用红色字体到第一个字符 // 将富文本字符串设置到单元格中 row.createCell(i).setCellValue(richText); } /** * 设置背景色 * * @param cell * @param color */ public static void setCellBackground(Cell cell, IndexedColors color) { CellStyle cellStyle = cell.getCellStyle(); cellStyle.setFillForegroundColor(color.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell.setCellStyle(cellStyle); }
自定义excel模板导出
最新推荐文章于 2024-09-29 11:08:44 发布