Apache POI Copy Cell
2015-08-15 22:35
579 查看
使用Java操作Excel表格,复制表格的功能。
import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; public class ExcelTool { private CreationHelper helper = null; private Workbook wb = null; public ExcelTool(Workbook workbook) { this.wb = workbook; this.helper = workbook.getCreationHelper(); } // 单元格复制函数 public void copyCell(Cell oldCell, Cell newCell) { CellStyle newStyle = this.wb.createCellStyle(); copyStyle(oldCell.getCellStyle(), newStyle); newCell.setCellStyle(newStyle); // 评论 if (oldCell.getCellComment() != null) { newCell.setCellComment(oldCell.getCellComment()); } // 对日期和超链接进行了处理 switch (oldCell.getCellType()) { case Cell.CELL_TYPE_BLANK: newCell.setCellType(Cell.CELL_TYPE_BLANK); break; case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(oldCell)) { newCell.setCellValue(oldCell.getDateCellValue()); } else { newCell.setCellValue(oldCell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); if (oldCell.getHyperlink() != null) { Font hlink_font = this.wb.createFont(); hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(IndexedColors.BLUE.getIndex()); Hyperlink link = helper.createHyperlink(Hyperlink.LINK_URL); link.setAddress(oldCell.getHyperlink().getAddress()); newCell.setHyperlink(link); newCell.getCellStyle().setFont(hlink_font); } break; } } // 单元格样式复制函数 private void copyStyle(CellStyle oldStyle, CellStyle newStyle) { newStyle.setAlignment(oldStyle.getAlignment()); newStyle.setDataFormat(oldStyle.getDataFormat()); newStyle.setFillPattern(oldStyle.getFillPattern()); newStyle.setHidden(oldStyle.getHidden()); newStyle.setLocked(oldStyle.getLocked()); newStyle.setIndention(oldStyle.getIndention()); newStyle.setRotation(oldStyle.getRotation()); newStyle.setVerticalAlignment(oldStyle.getVerticalAlignment()); newStyle.setWrapText(oldStyle.getWrapText()); } public void close() { try { this.wb.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- 前端开发时经常会被浏览器缓存搞得头大,apache设置一下就好了
- 前端开发时经常会被浏览器缓存搞得头大,apache设置一下就好了
- gitweb @ Apache [@Windows]
- Apache服务器多站点配置
- Apache服务器如何修改默认的文件根目录
- apache Apache winnt_accept: Asynchronous AcceptEx failed 错误的解决
- WAMP/LAMP环境搭配错误解决
- How To Setup a Rails 4 App With Apache and Passenger on CentOS 6
- org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected be
- apache2问题
- 配置安装Nagios启动apache时出现问题解决方法
- mac apache vhost 配置时出现的 404
- apache启动不了
- apache对域名的泛解析
- apache .htaccess 实现在URL后面自动加 / 利SEO操作
- Apache服务无法启动问题排查
- Hbase报错"org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.ipc.StandbyException): Operation ca"
- 将Apache的访问日志实时写入mysql数据库
- Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站
- 关于Apache的日志