zoukankan      html  css  js  c++  java
  • 使用Iterator遍历Sheet(POI)验证及解释结果有序性

    test.xlsx:

    Code:

    package poi;
    
    import static org.junit.Assert.*;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(value=Parameterized.class)
    public class TestIterator {
        public TestIterator(Integer times) {
        }
        
        @Parameters
        public static Collection<Integer[]> init(){
            int times=200;
            Integer[][] parameters=new Integer[times][1];
            for (int i = 0; i < times; i++) {
                parameters[i][0]=i;
            }
            return Arrays.asList(parameters);
        }
        
        @Test
        public void  testItertor() throws IOException{
            List<Integer> expected=new ArrayList<Integer>();//rowNums.need to change according to demand
            for (int i = 0; i <=1 ; i++) {
                expected.add(i);
            }
            expected.add(3);
            expected.add(4);
            expected.add(6);
            
            
            List<Integer> actual=new ArrayList<Integer>();
            
            String filePath="/poi/test.xlsx";
            InputStream is=this.getClass().getResourceAsStream(filePath);
            Workbook wb=new XSSFWorkbook(is);
            Sheet sheet=wb.getSheetAt(0);
            for (Row row : sheet) {
                actual.add(row.getRowNum());
            }
            
            assertEquals(expected, actual);
        }
    }

    结果:通过验证。

    源码解析:
    org.apache.poi.xssf.usermodel.XSSFSheet
    XSSFRow的存放数据结构:

     private TreeMap<Integer, XSSFRow> _rows;
        /**
         * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
         * be the third row if say for instance the second row is undefined.
         * Call getRowNum() on each row if you care which one it is.
         */
        @SuppressWarnings("unchecked")
        public Iterator<Row> rowIterator() {
            return (Iterator<Row>)(Iterator<? extends Row>) _rows.values().iterator();
        }
    
        /**
         * Alias for {@link #rowIterator()} to
         *  allow foreach loops
         */
        public Iterator<Row> iterator() {
            return rowIterator();
        }
  • 相关阅读:
    gray-code——找规律
    [LeetCode] Decode Ways 解码方法个数、动态规划
    操作系统之面试常考(转)
    国内90%以上的 iOS 开发者,对 APNs 的认识都是错的
    vim配置为IDE环境(超详细,极力推荐 git)
    curl的使用(from 阮一峰)
    图片鉴黄服务提供商
    转: 【理念篇】关于数据驱动运维的几点认识
    业务调度链的染色数据上报和关联
    ITIL的考核管理体系
  • 原文地址:https://www.cnblogs.com/softidea/p/4256332.html
Copyright © 2011-2022 走看看