解析excel

该篇博客展示了如何使用Java的StreamingReader库处理大型Excel文件,以减少内存消耗。通过监控堆内存使用情况,作者演示了如何设置缓存大小和行数以优化性能,并提供了一个读取并解析Excel数据的示例。博客还讨论了内存管理策略,包括最大内存使用情况的记录。
摘要由CSDN通过智能技术生成
package com.hgh;

import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * -Xmx100m -Xms100M
 *
 */
public class POIDemo3 {
    public static void main(String[] args) throws IOException {
        long maxUserMemory = 0;
        FileInputStream in = new FileInputStream("F://测试excel-50000.xlsx");
        long heapSize = Runtime.getRuntime().totalMemory();
        long heapMaxSize = Runtime.getRuntime().maxMemory();
        long heapFreeSize = Runtime.getRuntime().freeMemory();
        long memoryUse = heapSize - heapFreeSize;
        if (memoryUse >maxUserMemory){
            maxUserMemory = memoryUse;
        }
        System.out.println("heapsize"+formatSizeUtil.formatSize(heapSize));
        System.out.println("heapmaxsize"+formatSizeUtil.formatSize(heapMaxSize));
        System.out.println("heapFreesize"+formatSizeUtil.formatSize(heapFreeSize));
        System.out.println("使用内存"+formatSizeUtil.formatSize(memoryUse));
        Workbook wk = StreamingReader.builder()
                .rowCacheSize(100)  //缓存到内存中的行数,默认是10
                .bufferSize(8192)  //读取资源时,缓存到内存的字节大小,默认是1024
                .open(in);  //打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
        heapSize = Runtime.getRuntime().totalMemory();
        heapMaxSize = Runtime.getRuntime().maxMemory();
        heapFreeSize = Runtime.getRuntime().freeMemory();
        memoryUse = heapSize - heapFreeSize;
        if (memoryUse >maxUserMemory){
            maxUserMemory = memoryUse;
        }
        System.out.println("heapsize"+formatSizeUtil.formatSize(heapSize));
        System.out.println("heapmaxsize"+formatSizeUtil.formatSize(heapMaxSize));
        System.out.println("heapFreesize"+formatSizeUtil.formatSize(heapFreeSize));
        System.out.println("使用内存"+formatSizeUtil.formatSize(memoryUse));
        Sheet sheet = wk.getSheetAt(0);
        //表头数据
        List<String> sheetHeaderList = new ArrayList<>();
        //表数据体
        List<List<Integer>> sheetDataList = new ArrayList<>(sheet.getLastRowNum()-1);//指定容量,防止频繁扩容,移除表头

        //遍历所有的行
        for (Row row : sheet) {
           System.out.println("开始遍历第" + row.getRowNum() + "行数据:");
            //遍历所有的列
            StringBuilder builder = new StringBuilder();
            int rowIndex = row.getRowNum();
            if (rowIndex==0){
                for (Cell cell : row) {
                    builder.append(cell.getStringCellValue()).append(",");
                    sheetHeaderList.add(cell.getStringCellValue());
                }
            }else{
                List<Integer> rowData = new ArrayList<>(row.getLastCellNum());//指定容量,防止频繁扩容
                for (Cell cell : row) {
                    rowData.add(Integer.valueOf(cell.getStringCellValue()));
                    builder.append(cell.getStringCellValue()).append(",");
                }
                sheetDataList.add(rowData);
            }
            System.out.println("当前行数据为:" + builder.toString());
            heapSize = Runtime.getRuntime().totalMemory();
            heapMaxSize = Runtime.getRuntime().maxMemory();
            heapFreeSize = Runtime.getRuntime().freeMemory();
            memoryUse = heapSize - heapFreeSize;
            if (memoryUse >maxUserMemory){
                maxUserMemory = memoryUse;
            }
        }
            /*
            Row row = sheet.getRow(i);
            这类使用迭代器的流方式的数据,是不支持下标获取的,只能用迭代器获取.
                            public Row getRow(int rownum) {
                    throw new UnsupportedOperationException();
                }
             */
        /*
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            System.out.println("开始遍历第" + i + "行数据:");
            /*
            Row row = sheet.getRow(i);
            //遍历所有的列
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < row.getLastCellNum(); j++) {
                builder.append(row.getCell(j).getStringCellValue()).append(",");
            }
            System.out.println("当前行数据为:" + builder.toString());
        }*/
        heapSize = Runtime.getRuntime().totalMemory();
        heapMaxSize = Runtime.getRuntime().maxMemory();
        heapFreeSize = Runtime.getRuntime().freeMemory();
        if (memoryUse >maxUserMemory){
            maxUserMemory = memoryUse;
        }
        System.out.println("heapsize"+formatSizeUtil.formatSize(heapSize));
        System.out.println("heapmaxsize"+formatSizeUtil.formatSize(heapMaxSize));
        System.out.println("heapFreesize"+formatSizeUtil.formatSize(heapFreeSize));
        System.out.println("使用内存"+formatSizeUtil.formatSize(heapSize - heapFreeSize));
        System.out.println("最大使用内存=" + formatSizeUtil.formatSize(maxUserMemory));
        System.out.println(sheetHeaderList);
        System.out.println(sheetDataList);
        System.out.println("sheetDataList.size=" + sheetDataList.size());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值