Apache POI使用

1.导入坐标

XML 复制代码
        <!-- poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi}</version>
            </dependency>

2. 测试类

说明:在D盘生成excel文件

java 复制代码
package com.sky.test;

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.FileOutputStream;

/*
 * 使用POI操作Excel文件
 * */
public class POITest {


    public static void write () throws Exception {
        //  通过POI创建Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //excel文件创建一个sheet页
        XSSFSheet sheet = excel.createSheet("info");
       //在sheet创建一个行对象,rownum表示从第零行开始
        XSSFRow row = sheet.createRow(0);
        //创建单元格并且写入文件内容
        row.createCell(1).setCellValue("姓名");

        row.createCell(2).setCellValue("城市");
//        创建一个新行
         row = sheet.createRow(1);
        row.createCell(1).setCellValue("张飒");

        row.createCell(2).setCellValue("成都");
//        创建一个新行
        row = sheet.createRow(2);
        row.createCell(1).setCellValue("李四");

        row.createCell(2).setCellValue("重庆市");

//        输出流将内存的excel文件写入到磁盘
        FileOutputStream out = new FileOutputStream(new File("D:\\mm.xlsx"));
        excel.write(out);
//        关闭资源
        out.close();
        excel.close();
    }

    public static void main(String[] args) throws Exception {
     write();
    }
}

说明:在D盘读取文件

java 复制代码
   /*通过POI读取Excel文件中的内容
    * @throws Exception
    * */
    public  static  void  read() throws  Exception{
        FileInputStream in = new FileInputStream(new File("D:\\mm.xlsx"));
        //读取磁盘上存在excel文件
        XSSFWorkbook excel = new XSSFWorkbook(in);
   //读取excel文件中第一个Sheet文件
        XSSFSheet sheet = excel.getSheetAt(0);
 //获取sheet中最后一行的行号
        int lastRowNum = sheet.getLastRowNum();

        for(int i=1;i<=lastRowNum;i++){
//            获得每一行
            XSSFRow row = sheet.getRow(i);
//获得单元格对象
            String cellValue1 = row.getCell(1).getStringCellValue();
            String cellValue2 = row.getCell(2).getStringCellValue();
            System.out.println(cellValue1+""+cellValue2);

        }
        //关闭资源
        in.close();
        excel.close();
    }

    public static void main(String[] args) throws Exception {
//     write();
     read();
    }

3.展示

相关推荐
百锦再1 分钟前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
C++chaofan36 分钟前
项目中基于redis实现缓存
java·数据库·spring boot·redis·spring·缓存
百***864640 分钟前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
MZ_ZXD0011 小时前
springboot流浪动物救助平台-计算机毕业设计源码08780
java·spring boot·后端·python·spring·flask·课程设计
没有bug.的程序员1 小时前
Spring 全家桶在大型项目的最佳实践总结
java·开发语言·spring boot·分布式·后端·spring
掘金码甲哥1 小时前
🎨 新来的外包,在大群分享了它的限流算法的实现
后端
在坚持一下我可没意见1 小时前
Spring IoC 入门详解:Bean 注册、注解使用与 @ComponentScan 配置
java·开发语言·后端·spring·rpc·java-ee
用户21411832636021 小时前
Claude Skills实战指南:Skill Seekers 自动生成 SiliconFlow API 技能
后端
b***9101 小时前
【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
android·前端·后端·mybatis
leonardee1 小时前
Android和JAVA面试题相关资料
java·后端