Java Excel分割成许多小文件

最近在处理excel,数据很多,需要将excel拆分成许多小块,并保留原来的格式,于是写了该算法,并能保留原来的样式,使用很简单:

复制代码
Sheet splitSheet = ExcelUtil.split(sheet, 0, 20, 5, 8);

传入开始行、结束行、开始列、结束列即可

java 复制代码
    public static Sheet split(Sheet sheet, int startRow, int endRow, int startCol, int endCol) {
        Workbook workbook = new SXSSFWorkbook();
        Sheet newSheet = workbook.createSheet("Sheet1");

        for (int i = startRow; i <= endRow; i++) {
            Row tableDataRow = sheet.getRow(i);

            Row newRow = newSheet.createRow(i - startRow);
            if (tableDataRow == null) {
                continue;
            }

            for (int j = startCol; j <= endCol; j++) {
                Cell cell = tableDataRow.getCell(j);
                Cell newCell = newRow.createCell(j - startCol);

                CellStyle cellStyle = workbook.createCellStyle();
                cellStyle.cloneStyleFrom(cell.getCellStyle());

                newCell.setCellStyle(cellStyle);
                newCell.setCellValue(cell.getStringCellValue());

            }
        }

        for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
            int firstRow = mergedRegion.getFirstRow();
            int lastRow = mergedRegion.getLastRow();
            int firstColumn = mergedRegion.getFirstColumn();
            int lastColumn = mergedRegion.getLastColumn();

            if (firstRow >= startRow && lastRow <= endRow && firstColumn >= startCol && lastColumn <= endCol) {
                CellRangeAddress cellAddresses = new CellRangeAddress(firstRow - startRow, lastRow - startRow, firstColumn - startCol, lastColumn - startCol);
                newSheet.addMergedRegion(cellAddresses);
            }
        }
        return newSheet;
    }

测试代码

java 复制代码
    public static void main(String[] args) throws Exception {

        String path = "xxx.xlsx";
        String targetPath = "xxx1.xlsx";

        FileInputStream fis = null;

        File file = new File(path);
        try {
            fis = new FileInputStream(file);
            Workbook workbook = WorkbookFactory.create(fis);
            Sheet sheet = workbook.getSheetAt(0);
            Sheet splitSheet = ExcelUtil.split(sheet, 0, 20, 5, 8);
            ExcelUtil.saveSheet(targetPath,splitSheet);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

保存sheet工具类

java 复制代码
    public static void saveSheet(String path, Sheet sheet) throws IOException {
        File file = new File(path);
        FileOutputStream fos = new FileOutputStream(file);
        sheet.getWorkbook().write(fos);
        fos.close();
        sheet.getWorkbook().close();
    }
相关推荐
阿珊和她的猫2 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234174 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~5 小时前
C#---StopWatch类
开发语言·c#
桦说编程6 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen6 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研6 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员7 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋7 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cui__OaO8 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
阿华的代码王国8 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端