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();
    }
}
相关推荐
青浅l14 小时前
vue中回显word、Excel、txt、markdown文件
vue.js·word·excel
tang_jian_dong18 小时前
win10 配置下载安装 Office LTSC 专业增强版 2024
word·excel
SelectDB1 天前
从 OpenSearch 到 Apache Doris:领创集团日志系统升级实践,降本 45%
大数据·apache
SelectDB1 天前
货拉拉用户画像基于 Apache Doris 的数据模型设计与实践
数据库·apache
hmb↑1 天前
Apache Flink CDC——变更数据捕获
大数据·flink·apache
todoitbo2 天前
从大数据角度看时序数据库选型:Apache IoTDB的实战经验分享
大数据·apache·时序数据库·iotdb
CodeCraft Studio2 天前
国产化Excel处理控件Spire.XLS教程:使用Java将CSV转换为PDF(含格式设置)
java·pdf·excel·spire.xls·文档格式转换·csv转pdf
qq_334466862 天前
excel VBA应用
java·服务器·excel
E_ICEBLUE2 天前
快速合并 Excel 工作表和文件:Java 实现
java·microsoft·excel
祝威廉2 天前
运营好搭档:InfiniSynapse 多Excel文件关联分析
excel