poi处理excel基本操作时写的工具类
Posted lgqrlchinese
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi处理excel基本操作时写的工具类相关的知识,希望对你有一定的参考价值。
1.复制单元格
1 ... 2 public static void copyCell(Workbook wb, Cell srcCell, Cell distCell, boolean copyValueFlag) { 3 //新建单元格样式 4 CellStyle newStyle = wb.createCellStyle(); 5 //获取来源的单元格样式 6 CellStyle srcStyle = srcCell.getCellStyle(); 7 //将来源的单元格样式克隆到新的单元格样式 8 newStyle.cloneStyleFrom(srcStyle); 9 //样式 10 newStyle.cloneStyleFrom(newStyle); 11 distCell.setCellStyle(newStyle); 12 //克隆单元格的评论 13 if (srcCell.getCellComment() != null) { 14 distCell.setCellComment(srcCell.getCellComment()); 15 } 16 // 不同数据类型处理 17 CellType srcCellType = srcCell.getCellTypeEnum(); 18 distCell.setCellType(srcCellType); 19 if (copyValueFlag) { 20 if (srcCellType == CellType.NUMERIC) { 21 if (DateUtil.isCellDateFormatted(srcCell)) { 22 distCell.setCellValue(srcCell.getDateCellValue()); 23 } else { 24 distCell.setCellValue(srcCell.getNumericCellValue()); 25 } 26 } else if (srcCellType == CellType.STRING) { 27 distCell.setCellValue(srcCell.getRichStringCellValue()); 28 } else if (srcCellType == CellType.BLANK) { 29 30 } else if (srcCellType == CellType.BOOLEAN) { 31 distCell.setCellValue(srcCell.getBooleanCellValue()); 32 } else if (srcCellType == CellType.ERROR) { 33 distCell.setCellErrorValue(srcCell.getErrorCellValue()); 34 } else if (srcCellType == CellType.FORMULA) { 35 distCell.setCellFormula(srcCell.getCellFormula()); 36 } else { 37 } 38 } 39 } 40 ...
2.行复制
1 ... 2 /** 3 * 行复制功能 4 * 5 * @param wb 要复制到的Workbook 6 * @param fromRow 复制到的行 7 * @param toRow 来源的行 8 * @param copyValueFlag true的时候赋值里面的值 9 * @return [返回类型说明] 10 * @author 龙谷情 11 * @date 2020/6/9 19:42 12 * @exception/throws [异常类型] [异常说明] 13 * @since [v1.0] 14 */ 15 public static void copyRow(Workbook wb, Row fromRow, Row toRow, boolean copyValueFlag) { 16 toRow.setHeight(fromRow.getHeight()); 17 for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) { 18 Cell tmpCell = (Cell) cellIt.next(); 19 Cell newCell = toRow.createCell(tmpCell.getColumnIndex()); 20 //调用单元格赋值方法 21 copyCell(wb, tmpCell, newCell, copyValueFlag); 22 } 23 Sheet worksheet = fromRow.getSheet(); 24 //获取合并单元格的数量 25 int mergedRegionsNum = worksheet.getNumMergedRegions(); 26 for (int i = 0; i < mergedRegionsNum; i++) { 27 CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); 28 if (cellRangeAddress.getFirstRow() == fromRow.getRowNum()) { 29 CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(), (toRow.getRowNum() + 30 (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress 31 .getFirstColumn(), cellRangeAddress.getLastColumn()); 32 worksheet.addMergedRegionUnsafe(newCellRangeAddress); 33 } 34 } 35 } 36 ...
以上是关于poi处理excel基本操作时写的工具类的主要内容,如果未能解决你的问题,请参考以下文章