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;
                        }
                    }
                }
            }
        }
    }
相关推荐
迦蓝叶6 小时前
Apache Jena SPARQL 查询完全指南:入门与实战案例
apache·知识图谱·图搜索算法·三元组·jena·sparql·图查询
X@AKS17 小时前
解决使用EasyExcel导出带公式的excel,公式不自动计算问题
excel
Wang2012201317 小时前
wps excel中把特定几列除以某一列,然后以百分比显示
excel
LilySesy18 小时前
ABAP+在select的时候,可以A=B A=C B=C这样子JOIN吗?
数据库·sql·ai·excel·sap·abap
向上的车轮20 小时前
数据中台工作流编排引擎:Apache Airflow
apache
雾迟sec20 小时前
Web安全-文件上传漏洞-黑白名单及其它绕过思路(附思维导图)
javascript·安全·web安全·网络安全·apache·安全威胁分析
zhishidi20 小时前
Excel表格自适应大小设置方法
excel
yumgpkpm1 天前
CMP(类Cloudera CDP 7.3 404版华为泰山Kunpeng)和Apache Doris的对比
大数据·hive·hadoop·spark·apache·hbase·cloudera
zhangkaixuan4561 天前
Apache Paimon 查询全流程深度分析
java·apache·paimon
缺点内向1 天前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel