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();
    }
}
相关推荐
俊哥工具17 小时前
电脑怎么玩手机APP?超简单新手教程
pdf·电脑·word·excel·音视频
程序大视界20 小时前
【Python系列课程】Pandas(六):数据读写——CSV与Excel文件操作
python·excel·pandas
nvd111 天前
深度解析:Apache Beam YAML 部署至 GCP Dataflow 的架构与最佳实践
架构·apache
Land03291 天前
RPA 实现 Excel 自动化实操教程:批量数据处理零基础落地指南
自动化·excel·rpa
搬砖的小码农_Sky2 天前
Excel批量复制全攻略:从单列单行到高级场景
excel·人机交互
专注VB编程开发20年2 天前
淘宝上架销售技巧:Excel管理系统开发 / VBA / ERP / OA办公管理
java·数据库·excel
爱喝水的鱼丶2 天前
SAP-ABAP:SAP 简单报表输出开发系列(共6篇) 第五篇:SAP 报表多格式输出:Excel/PDF 批量导出功能实现
学习·性能优化·pdf·excel·sap·abap
tedcloud1232 天前
codegraph部署教程:构建代码库语义分析环境
服务器·人工智能·word·excel
吾爱神器2 天前
多个EXCEL工作表格合并数据列比对工具
excel·数据合并·数据对比·数据比对·excel数据合并·excel数据对比
27669582922 天前
拼多多m端/小程序 encrypt_info
java·小程序·apache·encrypt_info·encrypt_info解密·拼多多小程序·拼多多m端