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 小时前
基于Python的二手车价格数据分析与预测系统的设计与实现
开发语言·python·数据分析·预测系统
微笑尅乐2 小时前
力扣350.两个数组的交集II
java·算法·leetcode·动态规划
rzjslSe3 小时前
【JavaGuide学习笔记】理解并发(Concurrency)与并行(Parallelism)的区别
java·笔记·学习
青柠编程3 小时前
基于Spring Boot的竞赛管理系统架构设计
java·spring boot·后端
꒰ঌ 安卓开发໒꒱3 小时前
Java面试-并发面试(二)
java·开发语言·面试
Mr.Aholic3 小时前
Java系列知识之 ~ Spring 与 Spring Boot 常用注解对比说明
java·spring boot·spring
比特森林探险记4 小时前
Golang面试-Channel
服务器·开发语言·golang
xiangzhihong84 小时前
Spring Boot实现文字转语音功能
开发语言·python
月夕·花晨4 小时前
Gateway-断言
java·开发语言·分布式·spring cloud·微服务·nacos·sentinel
少陽君4 小时前
两个表格(Excel/CSV)字段不完全一致,要合并在一起
excel