Java OPI 两个workbook合并

Java OPI 两个workbook合并publicstaticHSSFWorkbookappendWorkBook(WorkbooktargetWorkBook,WorkbooksourceWorkbook){for(intj=0;j<sourceWorkbook.getNumberOfSheets();j++){SheetoldSheet=sourceWorkbook.getSheetAt(j);SheetnewSheet=

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;
    }

今天的文章Java OPI 两个workbook合并分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/24879.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注