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;
        }
  • 相关阅读:
    UITableViewCell分隔线
    Swift:Debug和Release状态下错误输出
    开发中遇到的那些坑
    code sign error 1
    Xcode头文件加锁
    UIPageControl显示图片
    关于RTKLIB资料整理和学习
    I2C中24C02从地址设置
    从库函数操作RCC的流程来理解偏移变量
    对于STM32别名区的理解 (转载)
  • 原文地址:https://www.cnblogs.com/yxth/p/13354305.html
Copyright © 2011-2022 走看看