POI复制行支持07,13。同时复制单元格合并
2017-05-29 14:25
471 查看
在网上找到了一个POI复制行的文章,可惜运行有问题,也不支持07和13。
自己修改了一下,添加了13格式xlsx的支持。使用最新版的poi,去掉过时的字段和方法。
重点是复制行的添加了复制单元格合并。
代码:
自己修改了一下,添加了13格式xlsx的支持。使用最新版的poi,去掉过时的字段和方法。
重点是复制行的添加了复制单元格合并。
代码:
/** * 行复制功能 * * @param fromRow * @param toRow */ public static void copyRow(Workbook wb, Row fromRow, Row toRow, boolean copyValueFlag) { toRow.setHeight(fromRow.getHeight()); for(Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) { Cell tmpCell = (Cell) cellIt.next(); Cell newCell = toRow.createCell(tmpCell.getColumnIndex()); copyCell(wb, tmpCell, newCell, copyValueFlag); } Sheet worksheet = fromRow.getSheet(); for(int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); if(cellRangeAddress.getFirstRow() == fromRow.getRowNum()) { CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(), (toRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress .getFirstColumn(), cellRangeAddress.getLastColumn()); worksheet.addMergedRegionUnsafe(newCellRangeAddress); } } } /** * 复制单元格 * * @param srcCell * @param distCell * @param copyValueFlag true则连同cell的内容一起复制 */ public static void copyCell(Workbook wb, Cell srcCell, Cell distCell, boolean copyValueFlag) { CellStyle newStyle = wb.createCellStyle(); CellStyle srcStyle = srcCell.getCellStyle(); newStyle.cloneStyleFrom(srcStyle); newStyle.setFont(wb.getFontAt(srcStyle.getFontIndex())); //样式 distCell.setCellStyle(newStyle); //评论 if(srcCell.getCellComment() != null) { distCell.setCellComment(srcCell.getCellComment()); } // 不同数据类型处理 CellType srcCellType = srcCell.getCellTypeEnum(); distCell.setCellType(srcCellType); if(copyValueFlag) { if(srcCellType == CellType.NUMERIC) { if(DateUtil.isCellDateFormatted(srcCell)) { distCell.setCellValue(srcCell.getDateCellValue()); } else { distCell.setCellValue(srcCell.getNumericCellValue()); } } else if(srcCellType == CellType.STRING) { distCell.setCellValue(srcCell.getRichStringCellValue()); } else if(srcCellType == CellType.BLANK) { } else if(srcCellType == CellType.BOOLEAN) { distCell.setCellValue(srcCell.getBooleanCellValue()); } else if(srcCellType == CellType.ERROR) { distCell.setCellErrorValue(srcCell.getErrorCellValue()); } else if(srcCellType == CellType.FORMULA) { distCell.setCellFormula(srcCell.getCellFormula()); } else { } } }
相关文章推荐
- ★★★同时支持iexplorer和firefox的javascript复制拷贝信息到剪贴板方法
- 优化IIS7.5支持10万个同时请求的配置方法
- 每日英语 2009 07 13
- oracle sql developer 如何支持多个窗体,打开多张表,多个tab,同时查看多个数据表
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求
- 设计并实现同时支持多种视频格式的流媒体点播系统
- java 上传文件代码,支持中文文件名和中文文件内容,可以同时提交多个参数
- Zend Framework 2 支持同时插入多条数据的Insert扩展
- 配置PHP使之能同时支持GIF和JPEG
- 让你的PHP同时支持GIF、png、JPEG
- 让Web API同时支持多个Get方法
- AntiXSS - 支持Html同时防止XSS攻击
- 同时支持多语言,多种数据库的多用博客提供下载 i-favourite 1.5
- xcode8同时支持Swift2.3和Swift3
- 让你的WINDOWS同时支持MYSQL4,MYSQL4.1,MYSQL5X
- CXF restful webserice同时支持几种协议 json, xml
- 新版python logging 封装,支持同时向console,file,socket输出
- 修改内核文件使系统同时支持多个特定usbserial设备
- 同时复制图片和文字到剪贴板
- 方便代理下单的EcStore收货地址一键分析插件,同时支持淘宝/京东/一号店