• POI读取excel中数据


    直接贴代码了,包括对于合并单元格的处理。

        public String[][] readSheet(Integer sheetId) {
    
            XSSFSheet sheet = workbook.getSheetAt(sheetId);
    
            int rowCount = sheet.getPhysicalNumberOfRows();
            String[][] data = new String[rowCount][];
            // 1. 遍历一遍数据
            for (int i = 0; i < rowCount; i++) {
                XSSFRow row = sheet.getRow(i);
                int colCount = row.getPhysicalNumberOfCells();
                data[i] = new String[colCount];
    
                for (int j = 0; j < colCount; j++) {
                    XSSFCell cell = row.getCell(j);
                    String value;
                    if (cell != null && StringUtils.isNotBlank(value = cell.getStringCellValue())) {
                        data[i][j] = value;
                    }
                }
            }
    
    
            // 2.解析所有的联合空格
            List<CellRangeAddress> list = sheet.getMergedRegions();
            for (CellRangeAddress cellAddresses : list) {
                int startRow = cellAddresses.getFirstRow();
                int endRow = cellAddresses.getLastRow();
                int startCol = cellAddresses.getFirstColumn();
                int endCol = cellAddresses.getLastColumn();
    
                // 2.1 起始位置数据
                String value = data[startRow][startCol];
    
                // 2.2 遍历所有位置数据
                if (StringUtils.isNotBlank(value)) {
                    for (int i = startRow; i <= endRow; i++) {
                        for (int j = startCol; j <= endCol; j++) {
                            data[i][j] = value;
                        }
                    }
                }
            }
            return data;
        }
  • 相关阅读:
    Objective-C Runtime 运行时之四:Method Swizzling
    App启动加载广告页面思路
    关于CoreData和SQLite多线程访问时的线程安全问题
    HIVE学习(待更新)
    流处理环境搭建
    CAJ2PDF
    ArcMap加载在线地图
    学习opencv(持续更新)
    风险和策略(待更新)
    区块链入门教程(转)
  • 原文地址:https://www.cnblogs.com/yxth/p/13354305.html
Copyright © 2020-2023  润新知