POI以及Excel的简单操作

本文介绍了Apache POI的基本概念,并通过实例详细讲解如何使用POI进行Excel的写入和读取操作。从创建Maven项目,引入依赖,到编写测试代码并展示运行结果,最后对读取操作进行了说明。


前言

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

一、POI是什么?

在这里插入图片描述
在这里插入图片描述

二、POI-Excel写入

1.建立一个maven项目

项目结构如下:
在这里插入图片描述

2.引入pom依赖

<dependencies> 
<!--xls(03)-->
 <dependency> 
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</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>

3.写操作测试代码

package com.hxm;

import com.sun.xml.internal.ws.resources.UtilMessages;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriteTest {

    String PATH="D:\\ideawork\\MyExcel\\hxm-poi";
    @Test
    public  void testWrite03() throws IOException {
        //创建一个工作簿
        Workbook workbook=new HSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");
        //创建一个行(1,1)
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日");
        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);

        //第二行
        Row row2 = sheet.createRow(1);
        Cell cell21 = row1.createCell(0);
        cell21.setCellValue("时间");
        //(2,2)
        Cell cell22 = row1.createCell(1);
        String time = new DateTime().toString("yyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表(IO流)
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完毕");



    }
}

4.运行结果

在这里插入图片描述
在文件夹里面有了新的.xls文件,显示了时间信息
在这里插入图片描述

三、POI-Excel读取

在包里面新建一个读取的测试类:ExcelReadTest.java
在这里插入图片描述
代码如下:

package com.hxm;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelReadTest {

    String PATH="D:\\ideawork\\MyExcel\\hxm-poi\\";
    @Test
    public  void testRead03() throws IOException {


        FileInputStream inputStream = new FileInputStream(PATH + "统计表03.xls");

        //1.创建一个工作薄。使用excel能操作的这边它都可以操作!
        Workbook workbook = new HSSFWorkbook(inputStream);
        //2.得到表
        Sheet sheet=workbook.getSheetAt(0);
        //3.得到行
        Row row =sheet.getRow(0);
        //4.得到列
        Cell cell=row.getCell(0);

        System.out.println(cell.getStringCellValue());
        inputStream.close();
            }
        }




运行,结果为:
在这里插入图片描述

总结

以上就是通过POI对表格的一个读写操作了,博主用的是03版本的,相较于07版本有一些弊端,03最多只有 65535 行,后缀为.xls文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值