一般通过XSSFCellStyle设置或者读取。
背景颜色的读取、设置,边框的设置
color的读取 XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("D:\\menu.xlsx")); XSSFSheet xssfSheet = wb.getSheetAt(0); XSSFRow xssfRow = xssfSheet.getRow(0); XSSFCell cell = xssfRow.getCell(j); XSSFCellStyle cellStyle = cell.getCellStyle(); XSSFColor color = cellStyle.getFillForegroundXSSFColor(); String color = color.getARGBHex(); // 前两位是透明度 color的设置 边框的设置 XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(i + 1); Cell cell = row.createCell(0); XSSFCellStyle cellStyle = wb.createCellStyle(); XSSFColor xssfColor = new XSSFColor(); xssfColor.setARGBHex(color); cellStyle.setFillForegroundColor(xssfColor); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 下边框 cellStyle.setBorderLeft(CellStyle.BORDER_THIN);// 左边框 cellStyle.setBorderTop(CellStyle.BORDER_THIN);// 上边框 cellStyle.setBorderRight(CellStyle.BORDER_THIN);// 右边框 cell.setCellStyle(cellStyle); cell.setCellValue(code);
// --- 数据有效性 下拉框选择 --- DataValidationHelper helper = new XSSFDataValidationHelper((XSSFSheet) sheet); XSSFDataValidationConstraint constraintBoolean = new XSSFDataValidationConstraint(textList); CellRangeAddressList regionsBoolean = new CellRangeAddressList(1, 500, 6, 11); DataValidation validationBoolean = helper.createValidation(constraintBoolean, regionsBoolean); validationBoolean.createErrorBox("输入值有误", "请从下拉框选择"); validationBoolean.setShowErrorBox(true); sheet.addValidationData(validationBoolean); // --- 数据有效性 只允许输入整数 --- DataValidationConstraint constraintNum = new XSSFDataValidationConstraint( DataValidationConstraint.ValidationType.INTEGER, DataValidationConstraint.OperatorType.GREATER_OR_EQUAL, "0"); CellRangeAddressList regionNumber = new CellRangeAddressList(1, 500, 4, 5); DataValidation validationNum = helper.createValidation(constraintNum, regionNumber); validationNum.createErrorBox("输入值类型出错", "数值型,请输入大于或等于0的整数值"); validationNum.setShowErrorBox(true); sheet.addValidationData(validationNum); // --- 数据有效性 只允许输入小数 --- DataValidationConstraint constraintDecimal = new XSSFDataValidationConstraint( DataValidationConstraint.ValidationType.DECIMAL, DataValidationConstraint.OperatorType.GREATER_OR_EQUAL, "0"); CellRangeAddressList regionDecimal = new CellRangeAddressList(1, 500, 3, 3); DataValidation validationDecimal = helper.createValidation(constraintDecimal, regionDecimal); validationDecimal.createErrorBox("输入值类型出错", "数值型,请输入大于或等于0的小数值"); validationDecimal.setShowErrorBox(true); sheet.addValidationData(validationDecimal);