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;
                        }
                    }
                }
            }
        }
    }
相关推荐
weixin_462446237 小时前
Python 解析 Excel 图表(Chart)信息实战:从 xlsx 中提取标题、字体和数据
python·数据分析·excel·报表自动化
沉默-_-7 小时前
微信小程序页面配置详解
学习·微信小程序·apache·微信开发者工具
薛定谔的猫喵喵7 小时前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
SeaTunnel8 小时前
Apache SeaTunnel MySQL CDC 支持按时间启动吗?
大数据·数据库·mysql·开源·apache·seatunnel
椎49510 小时前
苍穹外卖资源点整理+个人错误解析-Day12-数据统计-EXCEL报表
excel
喜欢吃豆11 小时前
从「文件URL」到「模型可理解内容」:一套完整的文件上传与解析处理流程详解(含PDF/Excel/图片)
pdf·大模型·excel
帆张芳显12 小时前
智表zcell产品V3.5 版发布,新增行列选中操作等功能
前端·javascript·excel·插件·canva可画
夜喵YM12 小时前
基于 Spire.XLS.Free for Java 实现无水印 Excel 转 PDF
java·pdf·excel
TheNextByte113 小时前
如何将Android联系人导出为 Excel 格式
android·excel
☀Mark_LY13 小时前
java读取excel文件返回JSON
java·json·excel