Java OPI 两个workbook合并

public static HSSFWorkbook appendWorkBook(Workbook targetWorkBook, Workbook sourceWorkbook) {
        for (int j = 0; j < sourceWorkbook.getNumberOfSheets(); j++) {
            Sheet oldSheet = sourceWorkbook.getSheetAt(j);
            Sheet newSheet = targetWorkBook.createSheet(oldSheet.getSheetName());
            mergeSheetAllRegion(oldSheet, newSheet);
            // 设置列宽
            int length = oldSheet.getRow(oldSheet.getFirstRowNum()).getLastCellNum();
            for (int i = 0; i <= length; i++) {
                newSheet.setColumnWidth(i, oldSheet.getColumnWidth(i));
            }
            CellStyle newstyle = targetWorkBook.createCellStyle();
            for (Iterator rowIt = oldSheet.rowIterator(); rowIt.hasNext(); ) {
                Row oldRow = (Row) rowIt.next();
                Row newRow = newSheet.createRow(oldRow.getRowNum());
                newRow.setHeight(oldRow.getHeight());
                for (Iterator cellIt = oldRow.cellIterator(); cellIt.hasNext(); ) {
                    Cell tmpCell = (Cell) cellIt.next();
                    Cell newCell = newRow.createCell(tmpCell.getColumnIndex());
                    // 复制单元格样式
                    newstyle.cloneStyleFrom(tmpCell.getCellStyle());
                    // 样式
                    newCell.setCellStyle(newstyle);
                    if (tmpCell.getCellComment() != null) {
                        newCell.setCellComment(tmpCell.getCellComment());
                    }
                    // 不同数据类型处理
                    CellType fromCellType = tmpCell.getCellType();
                    newCell.setCellType(fromCellType);
                    if (fromCellType == CellType.NUMERIC) {
                        if (DateUtil.isCellDateFormatted(tmpCell)) {
                            newCell.setCellValue(tmpCell.getDateCellValue());
                        } else {
                            newCell.setCellValue(tmpCell.getNumericCellValue());
                        }
                    } else if (fromCellType == CellType.STRING) {
                        newCell.setCellValue(tmpCell.getRichStringCellValue());
                    } else if (fromCellType == CellType.BLANK) {
                        // nothing21
                    } else if (fromCellType == CellType.BOOLEAN) {
                        newCell.setCellValue(tmpCell.getBooleanCellValue());
                    } else if (fromCellType == CellType.ERROR) {
                        newCell.setCellErrorValue(tmpCell.getErrorCellValue());
                    } else if (fromCellType == CellType.FORMULA) {
                        newCell.setCellFormula(tmpCell.getCellFormula());
                    } else {
                        // nothing29
                    }
                }
            }
        }
        return (HSSFWorkbook)targetWorkBook;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值