Java操作excel之POI和easyExcel 教程详解 狂神笔记

2023-11-09

1.应用场景

  1. 将用户信息导出为excel表格
  2. 讲Excel表中的信息录入到网站数据库,大大减小网站数据的录入量!

开发中经常会涉及到excel的处理,如导出Excel到数据库中!

操作Excel目前比较流行的就是Apache POI和阿里巴巴的easyExcel

2.Apache POI

简介

Apache POI官网: https://poi.apache.org/

在这里插入图片描述
HSSF 对应 Excel 03 版本,最多支持65535行

XSSF对应 Excel 07 版本,行数无限制

缺点:

  • 使用比较麻烦
  • 数据量大的时候会可能报OOM异常
项目准备

创建maven项目,作为父项目,去掉src目录
在这里插入图片描述
创建module模块,也是maven项目poi-study
在这里插入图片描述
导入依赖

<dependencies>
    <!--xLs(03)-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.2</version>
    </dependency>
    <!--xLsx(07)-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <!--日期格式化工具-->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.1</version>
    </dependency>
    <!--test-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

创建两个版本的Excel文件
在这里插入图片描述
打开可以看到,03版最多支持到65536行,而07版不受限制,理论上无限
在这里插入图片描述
二者文件名后缀不同,对应操作的Java工具类也不同

明确几个概念,工作簿、工作表、行、单元格,分别对应了各自的对象

在这里插入图片描述

代码演示

写测试,创建类

public class ExcelWriteTest {
   
    // 构建路径
    String PATH = "F:\\WorkSpace\\IDEA\\Test\\excel-study\\";
    @Test
    public void testWrite03() throws Exception {
   
        // 创建工作簿
        Workbook workbook = new HSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("考核成绩表");
        // 创建第一行
        Row row1 = sheet.createRow(0);// 第一行
        // 创建单元格
        Cell cell1 = row1.createCell(0);// 第一行的第一列
        cell1.setCellValue("数学");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue(100);
        // 第二行
        Row row2 = sheet.createRow(1);// 第一行
        Cell cell21 = row2.createCell(0);// 第一行的第一列
        cell21.setCellValue("时间");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        // 生成表,IO流,03版本使用xls后缀
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"考核成绩表03.xls");
        workbook.write(fileOutputStream);
        // 关闭流
        fileOutputStream.close();
        System.out.println("考核成绩表03输出完毕");
    }
    @Test
    public void testWrite07() throws Exception {
   
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("考核成绩表");
        // 创建第一行
        Row row1 = sheet.createRow(0);// 第一行
        // 创建单元格
        Cell cell1 = row1.createCell(0);// 第一行的第一列
        cell1.setCellValue("语文");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue(100);
        // 第二行
        Row row2 = sheet.createRow(1);// 第一行
        Cell cell21 = row2.createCell(0);// 第一行的第一列
        cell21.setCellValue("时间");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        // 生成表,IO流,07版本使用xlsx后缀
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"考核成绩表07.xlsx")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java操作excel之POI和easyExcel 教程详解 狂神笔记 的相关文章

随机推荐