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();
    }
}
相关推荐
IT古董7 小时前
【深度学习框架】MXNet(Apache MXNet)
深度学习·apache·mxnet
jukuya9 小时前
Apache HttpClient
爬虫·apache
疋瓞1 天前
excel实用问题:提取文字当中的数字进行运算
java·javascript·excel
小Tomkk1 天前
数据分析师使用Kutools for Excel 插件
信息可视化·excel
mqwguardain1 天前
Python分享10个Excel自动化脚本
python·自动化·excel
x-cmd1 天前
[250204] Mistral Small 3:小巧、快速、强大 | asdf 0.16.0 发布:Golang 重写带来性能飞跃
人工智能·ai·开源·apache·llama·包管理·mistral
流形填表2 天前
Excel to form ?一键导入微软表单
excel
亭台2 天前
【Excel笔记_4】平均绝对偏差(MAD,Mean Absolute Deviation)的EXCEL公式表达
笔记·excel
亭台2 天前
【Excel笔记_5】 LET 函数中数据范围不连续的处理方法
笔记·excel
保卫大狮兄2 天前
excel里面的数据怎样批量地处理,把数据竖排便成横排?
excel