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();
    }
}
相关推荐
bin91537 小时前
【EXCEL数据处理】000017 案例 Match和Index函数。
excel
zhangphil7 小时前
Windows环境Apache httpd 2.4 web服务器加载PHP8:Hello,world!
php·apache·httpd
shandianchengzi9 小时前
【记录】Excel|Excel 打印成 PDF 页数太多怎么办
pdf·excel
bin915312 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
一个散步者的梦19 小时前
Excel常用函数
excel
bin91531 天前
【EXCEL数据处理】000009 案列 EXCEL单元格数字格式。文本型数字格式和常规型数字格式的区别
大数据·前端·数据库·信息可视化·数据分析·excel·数据可视化
Eiceblue2 天前
Python保留数据删除Excel单元格的函数和公式
开发语言·python·excel
bin91532 天前
【EXCEL数据处理】000014 案例 EXCEL分类汇总、定位和创建组。附多个操作案例。
信息可视化·数据挖掘·数据分析·excel·数据可视化·数据图表·excel 数据分析
育种数据分析之放飞自我2 天前
GWAS分析中显著位点如何注释基因:excel???
linux·算法·excel
PowerBI学谦2 天前
Python in Excel 正式发布!
开发语言·python·excel