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;
                        }
                    }
                }
            }
        }
    }
相关推荐
hqyjzsb29 分钟前
2026年AI证书选择攻略:当“平台绑定”与“能力通用”冲突,如何破局?
大数据·c语言·人工智能·信息可视化·职场和发展·excel·学习方法
牛奔1 小时前
Linux 的日志分析命令
linux·运维·服务器·python·excel
java_logo3 小时前
Apache Flink Docker 容器化部署指南
docker·flink·apache·apache flink·apache flink部署·flink部署文档·flink部署教程
不吃葱的胖虎3 小时前
根据Excel模板,指定单元格坐标填充数据
java·excel
罗政3 小时前
【Excel批处理】一键批量AI提取身份证信息到excel表格,数据安全,支持断网使用
人工智能·excel
彭于晏Yan3 小时前
Apache Kafka使用
kafka·apache
晨晨渝奇4 小时前
pandas 中将两个 DataFrame 分别导出到同一个 Excel 同一个工作表(sheet1)的 A1 单元格和 D1 单元格
excel·pandas
木辰風4 小时前
EasyExcel根据动态字段,进行导出excel文件
java·前端·excel
彦楠4 小时前
Apache Commons Lang 中 Pair 类的使用教程
java·apache
Apache IoTDB4 小时前
Apache IoTDB 社区荣获开放原子开发者大会多项殊荣丨「开源、产学研用融合」双认可
开源·开放原子·apache·iotdb