java导出excel模板添加各种校验
添加值域(下拉列)
/*** @description 设置某些列的值只能输入预制的数据,显示下拉框.* @param sheet 模板sheet页(需要设置下拉框的sheet)* @param textList 下拉框显示的内容* @param firstRow 添加下拉框对应开始行* @param endRow 添加下拉框对应结束行* @param firstCol 添加下拉框对应开始列* @param endCol 添加下拉框对应结束列*/public void setHssfValidation(HSSFSheet sheet, String[] textList, int firstRow, int endRow, int firstCol, int endCol) {// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);// 加载下拉列表内容DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);// 数据有效性对象HSSFDataValidation dataValidationList = new HSSFDataValidation(regions, constraint);sheet.addValidationData(dataValidationList);}
添加批注(可用作必填项)
/*** @description 功能描述:构建一个带有批注的单元格* @param row: 当前的行对象* @param column: 创建单元格的列数(从0开始)* @param style: 样式对象* @param value: 内容值,可以为空*/private void buildTitleHssfCell(final HSSFRow row, int column, CellStyle style, String value, Boolean required,HSSFPatriarch drawingPatriarch) {HSSFCell c = row.createCell(column);// 设置样式 设置值c.setCellStyle(style);c.setCellValue(value);// 若为必填的话 设置批注if(required){HSSFComment cellComment = drawingPatriarch.createCellComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 3, 3,(short) 5, 6));cellComment.setAuthor("DOLPHIN");cellComment.setString(new HSSFRichTextString("必填项"));c.setCellComment(cellComment);}}