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·小程序
ftpeak4 分钟前
《Rust+Slint:跨平台GUI应用》第八章 窗体
开发语言·ui·rust·slint
森语林溪11 分钟前
大数据环境搭建从零开始(十七):JDK 17 安装与配置完整指南
java·大数据·开发语言·centos·vmware·软件需求·虚拟机
lsx20240640 分钟前
HTML 音频(Audio)详解
开发语言
woshihonghonga43 分钟前
【动手学深度学习】
开发语言·python
郝开1 小时前
Spring Boot 2.7.18(最终 2.x 系列版本)1 - 技术选型:连接池技术选型对比;接口文档技术选型对比
java·spring boot·spring
威风的虫1 小时前
ES6 数组方法:告别循环,拥抱函数式编程
开发语言·前端·javascript
码界筑梦坊1 小时前
240-基于Python的医疗疾病数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts
2301_803554521 小时前
C++ 锁类型大全详解
开发语言·c++
wuwu_q1 小时前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin