POI操作Excel表格系列3 --- 背景颜色、边框等属性的读取和设置以及数据有效性的添加
2016-01-10 12:16
645 查看
关于Excel表格的一些背景颜色的读取、边框的读取和设置,以及数据有效性的添加。
一般通过XSSFCellStyle设置或者读取。
背景颜色的读取、设置,边框的设置
数据有效性的添加
一般通过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);
相关文章推荐
- Incorrect information in file
- 新的开始2016-01-10
- 约瑟夫环问题
- ubuntu下安装R
- C语言中数组转化为字符串的方法
- C语言中数组转化为字符串的方法
- C语言中数组转化为字符串的方法
- orm2 中文文档 4. 定义关联
- centos6.5 RPM 包安装 Mysql
- Python基础01-04
- 从僵尸网络追踪到入侵检测 第4章 Honeyd动态模板防御扫描软件攻击
- 【四】搭建Markdown的编辑器
- 不同的OS
- [转]简明网络I/O模型---同步异步阻塞非阻塞之惑
- 基本数据类型的相互转换 int float NSinteger double 的相互转化
- 泛谈手机app测试
- 终审项目展示
- orm2 中文文档 3.3 模型钩子
- 24. Swap Nodes in Pairs leetcode Python 2016 new Season
- Centos7原生的repo源下载---备忘