POI操作Excel表格系列3 --- 背景颜色、边框等属性的读取和设置以及数据有效性的添加

关于Excel表格的一些背景颜色的读取、边框的读取和设置,以及数据有效性的添加。

 一般通过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);




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值