42 Apache poi实现excel中sheet的复制

java 复制代码
@RequestMapping("excel")
    public void excel(){
        String sourceFilePath = "D:\\source.xlsx"; // 原 Excel 文件路径
        String destinationFilePath = "D:\\destination.xlsx"; // 目标 Excel 文件路径
        String sheetNameToCopy = "Sheet1"; // 要复制的工作表名称
        try (FileInputStream sourceFile = new FileInputStream(sourceFilePath);
             XSSFWorkbook sourceWorkbook = new XSSFWorkbook(sourceFile);
             XSSFWorkbook destinationWorkbook = new XSSFWorkbook()) {

            XSSFSheet sourceSheet = sourceWorkbook.getSheet(sheetNameToCopy);
            if (sourceSheet == null) {
                System.out.println("Sheet " + sheetNameToCopy + " does not exist in the source file.");
                return;
            }

            // 创建一个新工作表
            XSSFSheet newSheet = destinationWorkbook.createSheet(sheetNameToCopy);

            // 复制源工作表的数据到新工作表
            copySheet(sourceSheet, newSheet);

            // 将新的工作簿写入到文件
            try (FileOutputStream fileOut = new FileOutputStream(destinationFilePath)) {
                destinationWorkbook.write(fileOut);
            }
            System.out.println("Sheet copied successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void copySheet(XSSFSheet sourceSheet, XSSFSheet destinationSheet) {
        // 复制行
        for (int i = 0; i <= sourceSheet.getLastRowNum(); i++) {
            Row sourceRow = sourceSheet.getRow(i);
            if (sourceRow != null) {
                Row newRow = destinationSheet.createRow(i);
                // 复制单元格
                for (int j = 0; j < sourceRow.getLastCellNum(); j++) {
                    Cell sourceCell = sourceRow.getCell(j);
                    if (sourceCell != null) {
                        Cell newCell = newRow.createCell(j);
                        // 复制单元格的内容
                        switch (sourceCell.getCellType()) {
                            case STRING:
                                newCell.setCellValue(sourceCell.getStringCellValue());
                                break;
                            case NUMERIC:
                                newCell.setCellValue(sourceCell.getNumericCellValue());
                                break;
                            case BOOLEAN:
                                newCell.setCellValue(sourceCell.getBooleanCellValue());
                                break;
                            case FORMULA:
                                newCell.setCellFormula(sourceCell.getCellFormula());
                                break;
                            case ERROR:
                                newCell.setCellErrorValue(sourceCell.getErrorCellValue());
                                break;
                            default:
                                newCell.setCellValue(sourceCell.toString());
                                break;
                        }
                    }
                }
            }
        }
    }
相关推荐
BillKu9 小时前
Java读取Excel日期内容
java·开发语言·excel
簪花走马过长安12 小时前
【PDF识别改名】使用京东云OCR完成PDF图片识别改名,根据PDF图片内容批量改名详细步骤和解决方案
ui·pdf·ocr·excel·京东云·wps·图片区域识别重命名
阿里云云原生13 小时前
Apache RocketMQ + “太乙” = 开源贡献新体验
apache·rocketmq
瓶子xf14 小时前
Excel制作玫瑰图
excel
Rinleren20 小时前
Apache部署
apache
家里有只小肥猫20 小时前
开通保存图片权限
apache
不讲废话的小白1 天前
给 Excel 整列空格文字内容加上前缀:像给文字穿衣服一样简单!
c语言·excel
weixin_443290692 天前
【脚本系列】如何使用 Python 脚本对同一文件夹中表头相同的 Excel 文件进行合并
开发语言·python·excel
谢尔登2 天前
office-ai整合excel
人工智能·excel