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;
                        }
                    }
                }
            }
        }
    }
相关推荐
徐同保11 小时前
vue 在线预览word和excel
vue.js·word·excel
kaixin_啊啊15 小时前
计算机二级office操作技巧——Excel篇
excel
檀越剑指大厂15 小时前
开源多场景问答社区论坛Apache Answer本地部署并发布至公网使用
开源·apache
nvd1116 小时前
Java ETL - Apache Beam 简介
java·apache·etl
~在杰难逃~1 天前
关于订单信息的Excel数据分析报告
笔记·数据分析·excel·数据分析报告
生产队队长1 天前
SpringBoot2:web开发常用功能实现及原理解析-整合EasyExcel实现Excel导入导出功能
spring boot·excel
麋鹿会飞但不飘1 天前
EasyExcel拿表头(二级表头)爬坑,invokeHeadMap方法
java·spring boot·excel
Eiceblue2 天前
Python 实现Excel XLS和XLSX格式相互转换
vscode·python·pycharm·excel
if时光重来2 天前
springboot项目实现导出excel动态设置表头
spring boot·后端·excel
我是Superman丶2 天前
【工具】Java Excel转图片
java·python·excel