Apache POI CellStyle cloneStyleFrom
2015-08-26 10:59
567 查看
问题描述
在使用 Apache POI-3.8的时候,需要一个功能,就是处理上传得 Excel的 cell style。如果数据有错误,则标红或者加上其他 style 标识。但是当直接获取到 cell 的 style 进行处理后,再 set 回去会发现很多其他的 cell 的 style 也被修改了。其实是因为在 Excel 中,多个 cell 会共用一个 style,这样会就不必为每个 cell存储一个 style。所以虽然我们只想修改一个 cell 的 style,其他 cell 也跟着变了。// 改一个style,其他的cell 的 style也跟着变了 Cell cell = row.getCell(cellIndex); CellStyle style = cell.getRow().getSheet().getWorkbook().createCellStyle(); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.RED.index); style.setLeftBorderColor(IndexedColors.RED.index); style.setRightBorderColor(IndexedColors.RED.index); style.setTopBorderColor(IndexedColors.RED.index); cell.setCellStyle(style);
解决方法
使用 POI提供的方法 - cloneStyleFrom 来克隆一个 style 出来专门为这个 cell 来设置 style。Cell cell = row.getCell(cellIndex); CellStyle style = cell.getRow().getSheet().getWorkbook().createCellStyle(); style.cloneStyleFrom(cell.getCellStyle()); // 克隆出一个 style style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.RED.index); style.setLeftBorderColor(IndexedColors.RED.index); style.setRightBorderColor(IndexedColors.RED.index); style.setTopBorderColor(IndexedColors.RED.index); cell.setCellStyle(style);
相关文章推荐
- 使用Apache Axis部署 Web服务时的常见问题及其解决方法
- 利用Apache AXIS 1 发布WebService
- Apache中按天分割日志
- Linux服务器部署系列之一—Apache篇(上)
- Apache设置 局域网访问
- 全新64位CentOS6.X上LAMP架构搭建备忘-apache的安装问题解决
- 如何从Apache官网下载windows版apache服务器
- 安装Apache时可能会遇到的问题
- apache常见问题处理
- web服务器的Apache架构
- mac在下面Apache 创 .htaccess档
- CentOS 上apache+Subversion搭建及常见问题处理
- linux下安装apache 及报错解答
- 构建Apache+Subversion的环境
- apache Solr 入门见解
- 使用Eclipse自带的Maven插件创建Web项目时报错:Could not resolve archetype org.apache.maven.archetypes:maven-archetyp
- LAMP环境搭建
- Apache服务环境在Linux中搭建详解
- Apache安装完成后添加模块
- apxs - Apache 扩展工具