java POI解析Excel大文件,获取表头

目录

  • 前言
  • 依赖
  • 代码
  • StreamingReader的open
  • WorkbookFactory的create
  • CSV解析首行

前言

poi解析大文件可能出现oom,通样大小文件,xlsx会oom,xls不会,所以使用流式的方式改造解析xlsx文件的代码。
我的需求是提取每一页的表头,所以值提取第一行,如果需要全部内容,把行数循环地方的if改成while就行。
产生Workbook给的入参都是给的File,没用Inputstream,原因见后两章

依赖

可能不全,按需引入更多poi相关依赖

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.monitorjbl</groupId>
            <artifactId>xlsx-streamer</artifactId>
            <version>2.2.0</version>
        </dependency>

代码

//newExcel true表示xlsx,false表示xls
private static String excel(File file, boolean newExcel) throws Exception {
        StringBuilder sb = new StringBuilder();
        Workbook workbook = null;
        try {
            if (newExcel) {
            	//我只取第一行,你们按需调大
                workbook = StreamingReader.builder()
                        .rowCacheSize(1)
                        .bufferSize(10240)
                        .open(file);
            } else {
            	//xls的还是普通方式
                workbook = WorkbookFactory.create(file);
            }
            getSheet(sb, workbook);
        } finally {
            if (workbook != null) {
                workbook.close();
            }
        }
        return sb.toString();
    }

    private static void getSheet(StringBuilder sb, Workbook workbook) throws IOException {
        try {
            sb.append(LEFT_MIDDLE_BRACKET);
            Iterator<Sheet> sheetIterator = workbook.iterator();
            while (sheetIterator.hasNext()) {
                Sheet sheet = sheetIterator.next();
                String sheetName = sheet.getSheetName();
                if (sheetName.startsWith(BTXA_MASK)) {
                    continue;
                }
                sb.append(sheetName).append(C_COLON);
                Iterator<Row> rowIterator = sheet.iterator();
                //想取所有行的人把if改成while
                if (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    firstLine(sb, row);
                    if (sheetIterator.hasNext()) {
                        sb.append(C_SEMICOLON);
                    }
                }
            }
            sb.append(RIGHT_MIDDLE_BRACKET);
        } finally {
            workbook.close();
        }
    }

    private static void firstLine(StringBuilder sb, Row row) {
        try {
            int numOfColumns = row.getLastCellNum();
            for (int j = 0; j < numOfColumns; j++) {
                Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                sb.append(getCell(cell));
                if (j != numOfColumns - 1) {
                    sb.append(C_COMMA);
                }
            }
        } catch (Exception e) {
            log.error("异常列", e);
            sb.append("异常列");
        }
    }

    private static String getCell(Cell cell) {
        // 获取单元格类型
        CellType cellType = cell.getCellType();
        // 根据单元格类型处理值
        String cellValue;
        switch (cellType) {
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA:
                try {
                    DataFormatter formatter = new DataFormatter();
                    cellValue = formatter.formatCellValue(cell);
                } catch (IllegalStateException | NumberFormatException e) {
                    cellValue = cell.getCellFormula();
                }
                break;
            case BLANK:
                // 空单元格
                cellValue = "";
                break;
            case ERROR:
                // 错误值
                cellValue = "ERROR (" + ErrorEval.getText(cell.getErrorCellValue()) + ")";
                break;
            default:
                cellValue = "特殊类型";
        }
        return cellValue;
    }

StreamingReader的open

open方法使用InputStream还需要创建临时文件,如果有本地文件,直接给file

    /**
     * Reads a given {@code InputStream} and returns a new
     * instance of {@code Workbook}. Due to Apache POI
     * limitations, a temporary file must be written in order
     * to create a streaming iterator. This process will use
     * the same buffer size as specified in {@link #bufferSize(int)}.
     *
     * @param is input stream to read in
     * @return A {@link Workbook} that can be read from
     * @throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the stream
     */
    public Workbook open(InputStream is) {
      StreamingWorkbookReader workbook = new StreamingWorkbookReader(this);
      workbook.init(is);
      return new StreamingWorkbook(workbook);
    }

    /**
     * Reads a given {@code File} and returns a new instance
     * of {@code Workbook}.
     *
     * @param file file to read in
     * @return built streaming reader instance
     * @throws com.monitorjbl.xlsx.exceptions.OpenException if there is an issue opening the file
     * @throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the file
     */
    public Workbook open(File file) {
      StreamingWorkbookReader workbook = new StreamingWorkbookReader(this);
      workbook.init(file);
      return new StreamingWorkbook(workbook);
    }

WorkbookFactory的create

create方法说明InputStream比file更占内存

    /**
     * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
     *  the given InputStream.
     *
     * <p>Your input stream MUST either support mark/reset, or
     *  be wrapped as a {@link BufferedInputStream}!
     *  Note that using an {@link InputStream} has a higher memory footprint
     *  than using a {@link File}.</p>
     *
     * <p>Note that in order to properly release resources the
     *  Workbook should be closed after use. Note also that loading
     *  from an InputStream requires more memory than loading
     *  from a File, so prefer {@link #create(File)} where possible.
     *
     *  @param inp The {@link InputStream} to read data from.
     *
     *  @return The created Workbook
     *
     *  @throws IOException if an error occurs while reading the data
     *  @throws EncryptedDocumentException If the Workbook given is password protected
     */
    public static Workbook create(InputStream inp) throws IOException, EncryptedDocumentException {
        return create(inp, null);
    }

    /**
     * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
     *  the given File, which must exist and be readable.
     * <p>Note that in order to properly release resources the
     *  Workbook should be closed after use.
     *
     *  @param file The file to read data from.
     *
     *  @return The created Workbook
     *
     *  @throws IOException if an error occurs while reading the data
     *  @throws EncryptedDocumentException If the Workbook given is password protected
     */
    public static Workbook create(File file) throws IOException, EncryptedDocumentException {
        return create(file, null);
    }

CSV解析首行

赠送一个csv方法

    private static String csv(File file) throws Exception {
        String encode = KkFileUtils.getFileEncode(file);
        log.info("编码检测:{}", encode);
        if ("Big5".equalsIgnoreCase(encode)) {
            encode = "GB2312";
        }
        String res = "";
        try (FileInputStream fo = new FileInputStream(file);
             InputStreamReader isr = new InputStreamReader(fo, encode);
             BufferedReader br = new BufferedReader(isr)) {
            String line = br.readLine();
            if (StringUtils.isNotBlank(line)) {
                res = line;
            }
        }
        return res;
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/575319.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

同态加密原理解析

目录 1.数学介绍2.使用多项式环进行加密2.1 私钥和公钥的产生2.2 加密2.3 解密 3.同态计算3.1 同态加法3.2 同态乘法 1.数学介绍 同态加密方案基于一个难以计算的问题Ring Learning with Errorsred。这些方案中的数据在加密和未加密时都用多项式表示。 这里举一个简单的多项式…

AWTK MODBUS Client channel 模型

名称&#xff1a;modbus_client_channel 功能&#xff1a;通过 modbus 协议访问远程 slave 设备上的数据&#xff0c;需要配合 modbus_client模型一起使用。用于将 modbus client 中的 channel 包装成view_model或者view_model_array 一般来说不需要&#xff0c;直接使用modbus…

docker常用基本命令

把jar包和 dockerfile文件放到同一目录下#构建Docker镜像 注意后面的 . 不能省略 docker build -t your-image-name .#运行并创建一个容器 docker run -d -p 8080:8080 --name container_name your-image-name# 停止容器 767fce4cb990 容器ID (容器名也可以) docker stop 767f…

柱形图“变个装”,跟上时尚步伐!

前言 职场中&#xff0c;日报、周报、月报、年度总结&#xff0c;都离不开图表的制作&#xff0c;而柱状图又是最常用的一种&#xff0c;怎样的柱状图&#xff0c;才能让领导更容易阅读&#xff0c;甚至是眼前一亮呢&#xff1f;今天小编就将为大家介绍一下如何借助葡萄城公司…

4月26日 阶段性学习汇报

1.毕业设计与毕业论文 毕业设计已经弄完&#xff0c;加入了KNN算法&#xff0c;实现了基于四种常见病的判断&#xff0c;毕业论文写完&#xff0c;格式还需要调整&#xff0c;下周一发给指导老师初稿。目前在弄答辩ppt&#xff08;25%&#xff09;。25号26号两天都在参加校运会…

六西格玛管理培训并未过气:深挖其现代价值与应用

在众多管理培训中&#xff0c;六西格玛管理培训因其卓越的成效和广泛的适用性而备受推崇。尽管有人认为六西格玛管理培训已经过时&#xff0c;但实际上&#xff0c;它在现代企业中仍具有不可忽视的价值和应用。深圳天行健六西格玛培训公司解析如下&#xff1a; 一、六西格玛管理…

1Panel - 现代化、开源的 Linux 服务器运维管理面板

产品介绍 1Panel 是一个现代化、开源的 Linux 服务器运维管理面板。 1Panel的官方网站&#xff1a;https://1panel.cn 1Panel的GitHub仓库&#xff1a;https://github.com/1Panel-dev/1Panel 体验环境&#xff1a;https://demo.1panel.cn 1Panel 特点 开源特性 Star 数…

详细解析什么是期权交易的获利方法

期权交易的获利方法 在期权交易之前进行充分的准备工作和风险评估是至关重要的。其中行情结构、策略方法、预期收益和风险评估&#xff0c;是期权交易成功的关键要素。它们能帮助我们更好地制定交易计划&#xff0c;控制风险&#xff0c;并追求稳定的利润。以下是对这四点的详…

比较好的平民衣服品牌有哪些?平价质量好短袖品牌推荐

随着气候变暖&#xff0c;夏天的持续时间似乎越来越长&#xff0c;短袖作为夏季的必备服装&#xff0c;受到了广大男士的青睐。然而&#xff0c;面对市场上众多的短袖品牌和不同的质量&#xff0c;大家都觉得选短袖的时候实在难以找到质量好且合适自己的。 选择合适的短袖确实…

SimCal(ECCV2020)

文章目录 AbstractMethodUsing Existing Long-tail Classification ApproachesLoss Re-weightingFocal Loss略 Proposed SimCal:Calibrating the ClassifierDual Head Inference Experiment创新 原文 代码 Abstract 本文主要研究了长尾分布下的实例分割问题&#xff0c;并提出…

Open CASCADE学习|一个点的坐标变换

gp_Trsf 类是 Open CASCADE Technology (OCCT) 软件库中的一个核心类&#xff0c;用于表示和操作三维空间中的变换。以下是该类的一些关键成员和方法的介绍&#xff1a; 成员变量&#xff1a; scale: Standard_Real 类型&#xff0c;表示变换的缩放因子。 shape: gp_TrsfFor…

网络安全之防范钓鱼邮件

随着互联网的快速发展&#xff0c;新的网络攻击形式“网络钓鱼”呈现逐年上升的趋势&#xff0c;利用网络钓鱼进行欺骗的行为越来越猖獗&#xff0c;对互联网的安全威胁越来越大。网络钓鱼最常见的欺骗方式就是向目标群体发送钓鱼邮件&#xff0c;而邮件标题和内容&#xff0c;…

用于肺结节分类的常规 EHR 的纵向多模态Transformer集成成像和潜在临床特征

Longitudinal Multimodal Transformer Integrating Imaging and Latent Clinical Signatures from Routine EHRs for Pulmonary Nodule Classification 摘要 该研究提出了一种基于Transformer 的多模态策略&#xff0c;用于将重复成像与常规电子健康记录&#xff08;EHRs&…

AbstractRoutingDataSource实现多数据源切换以及事务中无法切换问题

一、AbstractRoutingDataSource实现多数据源切换 为了实现数据源的动态切换&#xff0c;我们采用了AbstractRoutingDataSource结合AOP反射来自定义注解。通过这种机制&#xff0c;我们可以在运行时根据自定义注解来选择不同的数据源&#xff0c;从而实现灵活高效的数据访问策略…

C++内存分布 new和delete介绍

目录 C/C内存分布 栈区 堆区 静态区 常量区 C new和delete 分配空间形式对比 new delete与malloc free的区别 可不可以串着使用new和free呢 C/C内存分布 C的内存分布&#xff0c;大体上分为栈区 堆区 静态区 常量区 栈区 栈区是用于存储函数调用时的局部变量 函…

C语言中,如何判断两个数组是否包含相同元素?

在C语言中判断两个数组是否包含相同元素可以采用多种方法&#xff0c;其中最常见的方法是使用排序和比较两个数组的元素。在解释这个问题之前&#xff0c;我们需要了解一下C语言中的数组、排序算法和比较方法。 数组 数组是C语言中一种基本的数据结构&#xff0c;它是一系列相…

mysql的DDL语言和DML语言

DDL语言&#xff1a; 操作数据库&#xff0c;表等&#xff08;创建&#xff0c;删除&#xff0c;修改&#xff09;&#xff1b; 操作数据库 1&#xff1a;查询 show databases 2:创建 创建数据库 create database 数据库名称 创建数据库&#xff0c;如果不存在就创建 crea…

Linux论坛搭建

1.安装httpd服务 1.1安装httpd软件 [rootlocalhost yum.repos.d]# dnf install httpd 1.2.修改httpd的配置 [rootlocalhost yum.repos.d]# vim /etc/httpd/conf/httpd.conf 1.3.启动这个httpd服务,并查看它的状态 [rootlocalhost yum.repos.d]# systemctl start httpd [ro…

前端调用DRI后端API出现跨域资源共享(CORS)问题解决办法

目录 1. 引言2. 跨源资源共享和实现方法3. 在Django项目中配置django-cors-headers库Reference 1. 引言 在进行后端API开发时&#xff0c;有时会遇到“跨域资源共享 (CORS) 请求…被阻止“的错误&#xff0c;如图1所示。本文讲解如何在使用DRF&#xff08;Django REST Framewo…

什么是ISP,为什么跨境推荐ISP?

ISP&#xff0c;全称Internet Service Provider&#xff0c;即“互联网服务提供商”。它是为个人或企业提供访问、使用或参与互联网服务的组织&#xff0c;主要为用户提供互联网接入业务、信息业务和增值业务。ISP是经国家主管部门批准的正式运营企业&#xff0c;享受国家法律保…