本例子使用的是HSSF,为Excel2003提供处理方案。
设定为输入类型为数值
import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFDataValidationHelper; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.CellRangeAddressList; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType; /** * 设定为输入类型为数值 * @param firstRow * @param endRow * @param firstCol * @param endCol
* 注意:如果是一个单元格,需要firstRow = endRow, firstCol = endCol * @return */ public static HSSFDataValidation setDataValidation(int firstRow,int endRow,int firstCol,int endCol) { CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); //数值型,大于0 DVConstraint constraint = DVConstraint.createNumericConstraint(ValidationType.DECIMAL, OperatorType.GREATER_THAN, "0", null); //整数 1到100之间 // DVConstraint constraint = DVConstraint.createNumericConstraint(ValidationType.INTEGER, OperatorType.BETWEEN, "1", “100"); HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint);//add dataValidation.createErrorBox("输入值类型或大小有误", "数值型,请输入不小于0的数值"); dataValidation.createPromptBox("", null); dataValidation.setShowErrorBox(true); return dataValidation; }
设置为下拉列表选项
/** * 添加数据有效性检查. * @param sheet 要添加此检查的Sheet * @param firstRow 开始行 * @param lastRow 结束行 * @param firstCol 开始列 * @param lastCol 结束列 * @param explicitListValues 有效性检查的下拉列表 * @throws IllegalArgumentException 如果传入的行或者列小于0(< 0)或者结束行/列比开始行/列小
* 注意: 如果是一个单元格,需要 firstRow = lastRow , firstCol= lastCol */ public static void setValidationData(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol,String[] explicitListValues) throws IllegalArgumentException{ if (firstRow < 0 || lastRow < 0 || firstCol < 0 || lastCol < 0 || lastRow < firstRow || lastCol < firstCol) { throw new IllegalArgumentException("Wrong Row or Column index : " + firstRow+":"+lastRow+":"+firstCol+":" +lastCol); } if (sheet instanceof XSSFSheet) { XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet)sheet); XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper .createExplicitListConstraint(explicitListValues); CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList); validation.setSuppressDropDownArrow(true); validation.setShowErrorBox(true); sheet.addValidationData(validation); } else if(sheet instanceof HSSFSheet){ CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(explicitListValues); DataValidation validation = new HSSFDataValidation(addressList, dvConstraint); validation.setSuppressDropDownArrow(true); validation.setShowErrorBox(true); sheet.addValidationData(validation); } }
设置模板文件的输入项表格样式
/** * 设置模板文件的输入项表格样式 * @param wb * @return */ public static CellStyle setValueStyle(Workbook wb) { //The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook CellStyle style = wb.createCellStyle(); //对齐方式设置 style.setAlignment(CellStyle.ALIGN_LEFT); //边框颜色和宽度设置 style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // style.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); //设置背景颜色 // style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index); style.setFillBackgroundColor(HSSFColor.LIGHT_TURQUOISE.index); style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); //设置背景色 style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置自动换行 style.setWrapText(true); return style; }
POI HSSFColor 颜色索引对照表
参考:
Java读写Excel之POI超入门
POI生成excel带下拉
使用POI3.8 设置EXCEL2007的数据有效性
Apache POI使用详解