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();
    }
}
相关推荐
影子24016 小时前
Navicat导出mysql数据库表结构说明到excel、word,单表导出方式记录
数据库·mysql·excel
东方佑9 小时前
使用 Python 自动处理 Excel 数据缺失值的完整指南
开发语言·python·excel
課代表9 小时前
利用 Excel 函数随机抽取(附示例)
数据结构·excel
Agome9913 小时前
linux面试题
java·开发语言·excel
失去妙妙屋的米奇14 小时前
Python库与Excel
开发语言·python·excel
winfredzhang15 小时前
将 Markdown 表格结构转换为Excel 文件
python·excel·markdown
Moshow郑锴17 小时前
SpringBoot3+EasyExcel通过WriteHandler动态实现表头重命名
spring·excel·导出·阿里巴巴·easyexcel·speadsheet
課代表17 小时前
VBA 中正则表达式使用指南
数据库·mysql·正则表达式·excel
flying jiang18 小时前
excel 时间戳 转日期
excel
DDDiccc19 小时前
项目-苍穹外卖(十七) Apache POI+导出数据
apache