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();
    }
相关推荐
晓131314 小时前
第七章 【C语言篇:文件】 文件全面解析
linux·c语言·开发语言
愚者游世14 小时前
Delegating Constructor(委托构造函数)各版本异同
开发语言·c++·程序人生·面试·改行学it
一 乐14 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
KIKIiiiiiiii15 小时前
微信个人号API二次开发中的解决经验
java·人工智能·python·微信
梵刹古音15 小时前
【C语言】 指针基础与定义
c语言·开发语言·算法
80530单词突击赢15 小时前
SpringBoot整合SpringMVC全解析
java·spring boot·后端
Ekehlaft15 小时前
这款国产 AI,让 Python 小白也能玩转编程
开发语言·人工智能·python·ai·aipy
rit843249915 小时前
MATLAB中Teager能量算子提取与解调信号的实现
开发语言·matlab
开源技术15 小时前
Python GeoPandas基础知识:地图、投影和空间连接
开发语言·ide·python
vx1_Biye_Design15 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven