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();
    }
}
相关推荐
SeaTunnel26 分钟前
Apache SeaTunnel 2.3.13 版本前瞻:核心引擎变化和 AI ETL 趋势值得关注
数据仓库·人工智能·apache·etl·seatunnel·数据同步
C_心欲无痕5 小时前
使用 XLSX.js 导出 Excel 文件
开发语言·javascript·excel
ekkcole17 小时前
easyexcel2.2.10对本地文件的指定行或指定列添加样式
excel·easyexcel
城数派17 小时前
1901-2024年我国省市县三级逐年潜在蒸散发数据(Shp/Excel格式)
excel
门思科技18 小时前
ThinkLink批量操作功能详解:如何通过Excel高效管理物联网设备与配置
物联网·excel
scx_link20 小时前
使用 Excel 中的 “快速填充“
excel
蜜汁小强20 小时前
Vim简单配置: 加点颜色加点格式
编辑器·vim·excel
徒手千行代码无bug21 小时前
填充Excel
excel
认真学GIS21 小时前
日尺度地下水水位!全国11897个地下水动态监测站点2005-2021年日尺度地下水水位(地下水埋深)(EXCEL格式)数据
服务器·前端·excel
fengyehongWorld1 天前
Excel 超级表获取行列数据的方法
excel