Java使用POI读取Excel名称管理器

这篇具有很好参考价值的文章主要介绍了Java使用POI读取Excel名称管理器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

文章目的

本文主要介绍如何使用poi读取到Excel的名称管理器中的内容。并且定位到单元格。

在企业的开发中可能需要通过名称管理器定位到某个单元格,然后在单元格上生成签名。

环境配置

Java:Jdk1.8

poi:5.2.3

maven依赖(pom.xml):

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

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

        <!-- 以下依赖非必须,可根据项目情况选择性依赖 -->
        <!-- poi案例代码,可以去掉 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.15</version>
        </dependency>

        <!-- 使用slf4j 作为日志门面 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <!-- 使用 log4j2 的适配器进行绑定 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.9.1</version>
        </dependency>

        <!-- log4j2 日志门面 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.2</version>
        </dependency>
        <!-- log4j2 日志实面 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.2</version>
        </dependency>

        <!-- log4j2-异步日志依赖 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.4</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

实现思路

poi的WorkBook有个getNames方法可以读到名称。

Excel操作

Excel的名称在下图中新建

Java使用POI读取Excel名称管理器,Java,Java框架,excel,poi

参考代码

以下代码用于得到单元格引用对象(CellReference)

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

/**
 * POI-Excel-工具类<br>
 *
 * @author namelessmyth
 * @version 1.0
 * @date 2023/8/4
 */
@Slf4j
public class PoiExcelUtil {
    /**
     * 根据名称管理器中的名称得到单元格
     *
     * @param book Excel工作簿对象
     * @param nameName 名称的名称
     * @return Cell Excel单元格对象
     */
    public static Cell getNameCell(Workbook book, String nameName) {
        Cell result = null;
        if (book != null && StrUtil.isNotBlank(nameName)) {
            Name name = book.getName(nameName);
            if (name != null) {
                CellReference cr = new CellReference(name.getRefersToFormula());
                if (cr != null) {
                    result = getCell(book, cr);
                }
            }
        } else {
            throw new IllegalArgumentException("The input parameter of method can't be null");
        }
        return result;
    }

    /**
     * 根据单元格引用对象得到单元格
     *
     * @param book Excel工作簿对象
     * @param cr 单元格引用对象,例如:sheetName!$A$3
     * @return Cell Excel单元格对象
     */
    public static Cell getCell(Workbook book, CellReference cr) {
        Cell cell = null;
        if (book != null && cr != null) {
            Sheet sheet = null;
            if (StrUtil.isNotBlank(cr.getSheetName())) {
                sheet = book.getSheet(cr.getSheetName());
            }
            if (sheet == null) {
                throw new RuntimeException(String.format("Unable to find Sheet based on sheet name in CellReference! %s", cr.getSheetName()));
            }
            cell = getCell(sheet, cr);
        } else {
            throw new IllegalArgumentException("the input parameter of method can not be null!");
        }
        return cell;
    }

    public static Cell getCell(Sheet sheet, CellReference cr) {
        Cell cell = null;
        if (sheet != null && cr != null) {
            Row row = sheet.getRow(cr.getRow());
            if (row != null) {
                cell = row.getCell(cr.getCol());
            }
        } else {
            throw new IllegalArgumentException("the input parameter of method can not be null!");
        }
        return cell;
    }

    /**
     * 返回Excel名称管理器中所有名称对应的单元格引用
     *
     * @param filePath Excel文件路径
     * @return List<CellReference>
     */
    public static List<Cell> listNameCell(String filePath) throws IOException {
        List<Cell> result = null;
        if (StrUtil.isNotBlank(filePath)) {
            log.info("excelFilePath:{}", filePath);
            Workbook book = new XSSFWorkbook(new FileInputStream(filePath));
            result = listNameCell(book);
            book.close();
        }
        return result;
    }

    /**
     * 返回Excel名称管理器中所有名称对应的单元格引用
     *
     * @param book Excel工作簿对象
     * @return List<CellReference> Excel单元格引用对象列表
     */
    public static List<Cell> listNameCell(Workbook book) throws IOException {
        List<Cell> result = null;
        // 打开Excel文件
        if (book != null && book.getAllNames() != null) {
            result = new ArrayList<>(book.getAllNames().size());
            // 获取所有的名称管理器
            for (Name name : book.getAllNames()) {
                String refersToFormula = name.getRefersToFormula();
                if (StrUtil.isNotBlank(refersToFormula)) {
                    CellReference cr = new CellReference(refersToFormula);
                    if (cr != null) {
                        Cell cell = getCell(book, cr);
                        if (cell != null) {
                            result.add(cell);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 根据名称得到名称管理器中的名称单元格引用
     *
     * @param book Excel工作簿对象
     * @param nameName 名称的名称
     * @return CellReference Excel单元格引用对象
     */
    public static CellReference getCellReference(Workbook book, String nameName) {
        CellReference result = null;
        if (book != null && StrUtil.isNotBlank(nameName)) {
            Name name = book.getName(nameName);
            if (name != null) {
                result = new CellReference(name.getRefersToFormula());
            }
        }
        return result;
    }
}

在单元测试代码中通过CellReference来获取单元格。

CellReference中记录着sheet名字,行号,列号。文章来源地址https://www.toymoban.com/news/detail-624619.html

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

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

class PoiExcelUtilTest {
    String excelPath = "E:\\resource\\方正璞华\\方正璞华\\绩效考核\\项目开发-绩效考核-20230801.xlsx";

    /**
     * 测试,根据excel文件和名称管理器中的名称得到单元格
     * @throws IOException
     */
    @Test
    void getNameCell() throws IOException {
        Workbook book = new XSSFWorkbook(new FileInputStream(excelPath));
        Cell cell = PoiExcelUtil.getNameCell(book, "name1");
        System.out.println(StrUtil.toString(cell));
    }

    /**
     * 测试,根据excel文件和单元格引用得到单元格
     * @throws IOException
     */
    @Test
    void getCellByReference() throws IOException {
        Workbook book = new XSSFWorkbook(new FileInputStream(excelPath));
        Cell cell = PoiExcelUtil.getCell(book, new CellReference("版本!$A$4"));
        System.out.println(StrUtil.toString(cell));
    }

    /**
     * 测试,读出Excel名称管理器中的所有单元格
     * @throws IOException
     */
    @Test
    void listNameCell() throws IOException {
        List<Cell> cells = PoiExcelUtil.listNameCell(excelPath);
        System.out.println(StrUtil.toString(cells));
    }
}

到了这里,关于Java使用POI读取Excel名称管理器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用POI实现JAVA操作Excel文件

    https://poi.apache.org/components/index.html xls最大支持65536行、256列 xlsx最大支持1048576行、16384列 poi 操作xls poi-ooml操作xlsx 优点:过程中写入缓存,不操作磁盘,最后再一次性导入磁盘,速度快+ 缺点:但是只能写入65535条数据,超过就会报异常 优点:可以写较大数据量(超过655135条)

    2024年02月14日
    浏览(30)
  • 【Java】使用POI按模板样式导出Excel

    根据模板样式进行excel导出。 首先,当然是要有一个excel模板,excel的样式用wps等进行设置。 然后就是代码的实现了,先引入POI的依赖: 然后就是实现方法里的代码,首先定义响应信息: 然后将excel模板转为输入流,这一步的实现方法有很多,具体选择因人而异,我这里就举

    2024年02月14日
    浏览(32)
  • java中使用POI生成Excel并导出

    注:本文章中代码均为本地Demo版本,若后续代码更新将不会更新文章 根据从数据库查询出的数据,将其写入excel表并导出 我的想法是通过在实体属性上写自定义注解的方式去完成。因为我们在代码中可以通过反射的方式去获取实体类中全部的注解及属性名称等等。我们可以

    2024年02月16日
    浏览(35)
  • Java使用POI解析带图片的excel,简洁好用

            这天遇到这个这样的需求,需要导入表格中的数据,还得支持带图片;这应该难不倒咱阿里的EasyExcel,打开官网文档一看哦豁,明确表示暂时不支持解析带图片的Excel...... 好了,这下只能看POI了,然后想起来项目里引入的HuTools工具类,它应该封装好了吧;于是决定用

    2024年02月11日
    浏览(29)
  • Java中Excel文件解析(POI简介及基本使用)

    在Java技术生态圈中,可以进行Excel文件处理的主流技术包括: Apache POI 、 JXL 、 Alibaba EasyExcel 等。 其中各个技术都有最适合的场景 Apache POI 基于 DOM 方式进行解析,将文件直接加载内存,所以速度较快,适合 Excel 文件数据量不大的应用场景。 JXL 只支持Excel 2003以下版本,所以

    2024年02月08日
    浏览(33)
  • Apache POI及easyExcel读取及写入excel文件

    目录 1.excel 2.使用场景 3.Apache POI 4.easyExcel 5.总结 1.excel excel分为两版,03版和07版。 03版的后缀为xls,最大有65536行。 07版的后缀为xlsx,最大行数没有限制。 2.使用场景 将用户信息导出到excel表格中。 将excel中的数据读取到数据库中。 3.Apache POI (1)说明 Apache POI是Apache软件基金会

    2024年02月06日
    浏览(35)
  • JAVA:使用POI SXSSFWorkbook方式导出Excel大数据文件

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java对Microsoft Office格式档案读和写的功能。POI组件可以提供Java操作Microsoft Office的API,导出格式为Office 2003时POI调用的HSSF包,导出格式为Office 2007时,调用XSSF包,而SXSSF包是POI3.8版本之上对XSSF的一个扩展,用

    2024年02月11日
    浏览(36)
  • Apache POI实现Excel导入读取数据和写入数据并导出

    Apache POI POI介绍 Apache POI是用Java编写的免费开源的跨平台的Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,其中使用最多的就是使用POI操作Excel文件。 maven坐标: POI结构: 入门案例 ExcelTest .java文件 从Excel文件读取数据

    2024年02月12日
    浏览(32)
  • 自动化数据驱动|Easy POI读取接口测试用例的excel

    在做的接口自动化测试中,把测试用例维护在excel表格中,通过Easy POI把用例中的行转成实体类对象,再通过testNG的@DataProvider注解把测试用例数据传递给测试方法执行用例。 2.1 用例表格示例 2.2 实体类设计 easy poi: 通过@Excel属性能够和excel的表头映射上去 ⚠️值的注意:实体

    2024年01月23日
    浏览(41)
  • java poi导入Excel、导出excel

    java poi导入Excel、导出excel ReadPatientExcelUtil PoiUtils FileUtils

    2024年02月15日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包