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();
    }
相关推荐
BBB努力学习程序设计3 分钟前
Java方法详解:提升代码复用性与可读性的利器
java
BBB努力学习程序设计3 分钟前
Java运算符完全指南:让代码学会“计算”和“判断”
java
csbysj202032 分钟前
DOM 节点
开发语言
用户849137175471638 分钟前
ThreadLocal 源码深度解析:JDK 设计者的“妥协”与“智慧”
java·后端
用户03048059126340 分钟前
# 【Maven避坑】源码去哪了?一文看懂 Maven 工程与打包后的目录映射关系
java·后端
小尧嵌入式1 小时前
C++基础语法总结
开发语言·c++·stm32·单片机·嵌入式硬件·算法
@游子1 小时前
Python学习笔记-Day2
开发语言·python
v***55341 小时前
springboot使用logback自定义日志
java·spring boot·logback
qq_336313931 小时前
java基础-集合进阶
java·开发语言·windows
稚辉君.MCA_P8_Java1 小时前
Gemini永久会员 归并排序(Merge Sort) 基于分治思想(Divide and Conquer)的高效排序算法
java·linux·算法·spring·排序算法