zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    什么是缓存?
    什么是反射?
    Nginx配置文件详解
    数据库插入,修改出现中文乱码解决办法
    hadoop的HA机制+zookeeper
    cascading--wordcount
    心情3
    完成用户登录注册功能
    感慨1
    Swing程序设计-初级
  • 原文地址:https://www.cnblogs.com/yxth/p/13354305.html
Copyright © 2011-2022 走看看