Apache POI操作excel

使用Apache POI

引入坐标

XML 复制代码
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

样例工程

java 复制代码
package com.sky.test;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class POITest {
    /**
     * 基于POI读取Excel文件
     * @throws Exception
     */
    public static void read() throws Exception{
        FileInputStream in = new FileInputStream(new File("D:\\itcast.xlsx"));
        //通过输入流读取指定的Excel文件
        XSSFWorkbook excel = new XSSFWorkbook(in);
        //获取Excel文件的第1个Sheet页
        XSSFSheet sheet = excel.getSheetAt(0);

        //获取Sheet页中的最后一行的行号
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 0; i <= lastRowNum; i++) {
            //获取Sheet页中的行
            XSSFRow titleRow = sheet.getRow(i);
            //获取行的第2个单元格
            XSSFCell cell1 = titleRow.getCell(1);
            //获取单元格中的文本内容
            String cellValue1 = cell1.getStringCellValue();
            //获取行的第3个单元格
            XSSFCell cell2 = titleRow.getCell(2);
            //获取单元格中的文本内容
            String cellValue2 = cell2.getStringCellValue();

            System.out.println(cellValue1 + " " +cellValue2);
        }

        //关闭资源
        in.close();
        excel.close();
    }

    public static void main(String[] args) throws Exception {
        read();
    }
}
相关推荐
文人sec4 小时前
玩转excel数据透视表和图表
excel
leihefeng8 小时前
PX06-对比两个 Excel 文件的差异:从逐单元格到生产级实现
python·excel
我的愿望是成为富婆12 小时前
HR数字化校招技术栈拆解:SQL、Excel、Power BI和AI工具在2026届JD中的真实权重
人工智能·sql·excel
泡海椒13 小时前
JQuick-Excel 项目定位与适用场景:声明式 Java Excel 的边界
java·开发语言·excel
96245613 小时前
从 Excel 营业流水到可验证测试订单:测试数据导入工具工程复盘
excel
用户03321266636714 小时前
Python 实现 Excel 转 XML:快速上手
python·excel
asdzx6715 小时前
Excel 水印方案对比:页眉图片 vs 背景图片(附 C# 实现)
c#·excel
Data-Miner1 天前
怎么用AI给Excel去重?先核对这7条再下手,别让好数据被删错:数以轻舟方案解析
人工智能·excel
Data-Miner1 天前
做表格数据分析的AI工具怎么选?先对一下这三个真实需求,再看哪些真能落地
人工智能·数据分析·excel
尤鸟倦8 天前
OneNote table复制到Excel遇到的各种问题
excel