ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java读取Excel表格大文件

2022-06-02 10:33:04  阅读:172  来源: 互联网

标签:map 遍历 Java 读取 iterator Excel list key poi


使用技术:

处理大量Excel数据

这里提供思路,大致情况还需要看需求,

读取少量数据也可以使用poiExcel或者excelExcel,当使用大量数据时,我的是70万条,普通的方法会报内存溢出。

pom.xml

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.0</version>
        </dependency>

        <!-- 读取大量excel数据时使用 -->
        <dependency>
            <groupId>com.monitorjbl</groupId>
            <artifactId>xlsx-streamer</artifactId>
            <version>2.1.0</version>
        </dependency>

代码:

public class SanuSupportInfo {
    private static SXSSFWorkbook wb;

    @Test
    public void getex() throws IOException {
        boolean isTitle = true; // 用于跳过标题
        String path = "xxxx.xlsx"; // 文件地址

        FileInputStream in = new FileInputStream(path);
        Workbook wk = StreamingReader.builder()
                .rowCacheSize(100)  //缓存到内存中的行数,默认是10
                .bufferSize(4096)  //读取资源时,缓存到内存的字节大小,默认是1024
                .open(in);  //打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
        Sheet sheet = wk.getSheetAt(0);    //取第0张表


        //遍历所有的行
        for (Row row : sheet) {
            if (isTitle) {    // 跳过第一行标题,需要标题时可以注释掉判断
                isTitle = false;
                continue;
            }
            System.out.println("开始遍历第" + row.getRowNum() + "行数据:");

            Map<Integer, String> map = new HashMap<>();
            List<String> list = new ArrayList<>(15); // 存储一行的值,一行多少列自己定,我的是15列,也可以存到map,自己决定

            //遍历所有的列
            Set<Map.Entry<Integer, Cell>> entries = ((StreamingRow) row).getCellMap().entrySet();
            Iterator<Map.Entry<Integer, Cell>> iterator = entries.iterator();

            // 遍历得到键值对,存入新的map中
            while (iterator.hasNext()) {
                Map.Entry<Integer, Cell> next = iterator.next();
                Integer key = next.getKey();
                String value = next.getValue().getStringCellValue();
                map.put(key, value);    //这一步很重要啊,当表中存在空值时,是直接跳过的,所以要用key记录一下索引,后面遍历使用
            }
            // 遍历map,存入list
            for (int i = 0; i < 15; i++) {
                String content = map.get(i);
                if(content==null){   // 避免出现空的情况,因为当表含有空数据时是直接跳过的,所以key存储索引就起到了作用
                    list.add(null);
                }else{
                    list.add(content);
                }
            }
        }
    }

经过上面代码后,一行的数据就存到了list中了,若要存储每一行数据的集合,可以把list存到集合中,然后在获取

标签:map,遍历,Java,读取,iterator,Excel,list,key,poi
来源: https://www.cnblogs.com/Retired-lad/p/16336848.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有