Java通过Apache POI操作Excel

1、添加依赖

xml 复制代码
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

2、读 EXCEL

java 复制代码
	public static void read() throws Exception{
        FileInputStream stream = new FileInputStream("D:\\Test\\file.xlsx");
        // 1.创建工作簿对象,并指定操作的文件
        Workbook workbook = new XSSFWorkbook(stream);
        // 2.获取工作表对象,既可以根据工作表的顺序获取,也可以根据工作表的名称获取
        Sheet sheet = workbook.getSheet("Sheet0");
        // 获取行数(获取当前工作表最后一行的行号,行号从0开始)
        int lastRowNum = sheet.getLastRowNum();
        // 获取列数
        Row row1 = sheet.getRow(0);
        int colNum = row1.getPhysicalNumberOfCells();
        // 3.遍历工作表获得行对象
        for(int i=0; i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            for (int j = 0; j < colNum; j++) {
                Cell cell = row.getCell(j);
                // 处理数字值
                if (cell.getCellType() == CellType.NUMERIC) {
                    double numericValue = cell.getNumericCellValue();
                    System.out.println(numericValue);
                } else if (cell.getCellType() == CellType.STRING) {
                    // 处理字符串值
                    String stringValue = cell.getStringCellValue();
                    System.out.println(stringValue);
                }
            }
        }
    }

注意:

1、操作的 EXCEL 对象要区分 .xls文件 还是 .xlsx 文件

java 复制代码
   FileInputStream stream = new FileInputStream("D:\\Test\\file.xlsx");
 	// XSSF操作的是Excel2007以上的版本,对应文件的后缀名是xlsx 
   Workbook workbook = new XSSFWorkbook(inputStream)  
 	// HSSF操作的是Excel2003以前的版本,对应的文件后缀名是xls
   Workbook workbook = new HSSFWorkbook(inputStream)

2、读取单元格数据要注意数据格式

读取单元格数据之前,可以先检查该单元格的数据类型。如果它是数字类型,那么你应该读取它的数值值;如果它是文本类型,那么你应该读取它的字符串值。这样可以避免数据类型不匹配的问题。

java 复制代码
 Cell cell = row.getCell(columnIndex);   
  // 处理数字值    } 
 if(cell.getCellType() == CellType.NUMERIC) {
   	double numericValue = cell.getNumericCellValue();
  // 处理字符串值   
 else if (cell.getCellType() == CellType.STRING) {
    String stringValue = cell.getStringCellValue();
  } 

2、写 EXCEL

java 复制代码
    public static void write(Workbook workbook,String path) throws Exception {
        /*创建一个表*/
        Sheet sheet = workbook.createSheet("Sheet1");
        /*创建一行*/
        Row row1 = sheet.createRow(0);
        /*创建一个单元格*/
        Cell cell = row1.createCell(0);
        /*设置单元格的内容*/
        cell.setCellValue("学习的内容");
        cell = row1.createCell(1);
        cell.setCellValue("java操作ecxel");
        Row row2 = sheet.createRow(1);
        cell = row2.createCell(0);
        cell.setCellValue("学习的时间");
        cell = row2.createCell(1);
        cell.setCellValue("2025-03-14 15:35:26");
        FileOutputStream fileOutputStream = new FileOutputStream(path);
        workbook.write(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

3、主程序调用

java 复制代码
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook();//xlsx
        write(workbook,"D:\\Test\\file.xlsx");
        read();
    }
相关推荐
二哈赛车手8 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
栗子~~8 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
程序员敲代码吗8 小时前
Go语言中Channel的实现与内存通信机制详解
excel
YDS8298 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
未若君雅裁10 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
AI人工智能+电脑小能手10 小时前
【大白话说Java面试题 第65题】【JVM篇】第25题:谈谈对 OOM 的认识
java·开发语言·jvm
阿维的博客日记11 小时前
Nacos 为什么能让配置动态生效?(涉及 @RefreshScope 注解)
java·spring
雨辰AI11 小时前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
辰海Coding12 小时前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
小小编程路12 小时前
C++ 多线程与并发
java·jvm·c++