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();
    }
}
相关推荐
lczdyx7 小时前
一键净化Excel数据:高性能Python脚本实现多核并行清理
python·excel·pandas·数据清洗·数据处理·自动化办公·openpyxl
wtsolutions8 小时前
Excel to JSON 插件 2.4.0 版本更新
json·excel
wb18921 小时前
shell脚本的条件测试
开发语言·python·excel
Apache IoTDB1 天前
Apache IoTDB V2.0.3 发布|新增元数据导入导出脚本适配表模型功能
apache·iotdb
lczdyx2 天前
高效Excel数据净化工具:一键清除不可见字符与格式残留
python·excel·pandas·数据清洗·数据处理
姜太小白2 天前
【Office】Excel两列数据比较方法总结
excel
jie188945758662 天前
ubuntu中,文本编辑器nano和vim区别,vim的用法
ubuntu·vim·excel
前端sweetGirl2 天前
Excel 中的SUMIFS用法(基础版),重复项求和
excel
CodeCraft Studio2 天前
国产化Excel处理组件Spire.XLS教程:如何使用 C# 将 Excel(XLS 或 XLSX)文件转换为 PDF
pdf·c#·excel
霸王蟹2 天前
React 项目中封装 Excel 导入导出组件:技术分享与实践
前端·笔记·学习·react.js·typescript·excel·vite