前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【在线教育】POI入门

【在线教育】POI入门

作者头像
陶然同学
发布2023-02-27 13:52:14
1.3K0
发布2023-02-27 13:52:14
举报
文章被收录于专栏:陶然同学博客

文章目录

3.POI入门(了解)

3.1 POI 概述

3.1.1 简介

3.1.2 官网

3.2 入门案例

3.2.1 环境搭建

3.2.2 xls文件写操作

3.2.3 xlsx 文件写操作

3.2.4 xls 文件读操作

3.2.5 xlsx 文件读操作

3.2.6 读取不同类型的数据

3.POI入门(了解)

3.1 POI 概述

3.1.1 简介

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

功能

描述

HSSFWorkBook

提供读写Microsoft Excel格式档案的功能,xls文档

XSSFWorkBook

提供读写Microsoft Excel OOXML格式档案的功能,xlsx文件

HWPF

提供读写Microsoft Word格式档案的功能

HSLF

提供读写Microsoft PowerPoint格式档案的功能

HDGF

提供读写Microsoft Visio格式档案的功能

3.1.2 官网

Apache POI - the Java API for Microsoft Documents

3.2 入门案例

3.2.1 环境搭建 创建项目:

修改pom

代码语言:javascript
复制
<dependencies>
        <!--xls-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <!--xlsx-->
        <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.2.2 xls文件写操作

  • excel2003 文件扩展名为 xls
  • 名词:
    • 工作簿:一个excel文件,就是一个工作簿
    • 工作表:一个工作簿中,可以所有多个工作表Sheet
    • 行:每一个工作表,包含多行row
    • 单元格:每行有多个单元格Cell组成。
代码语言:javascript
复制
package com.zx.poi;
​
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.FileOutputStream;
import java.io.IOException;
​
public class Excel03Test {
​
    @Test
    public void testWrite03() throws IOException {
​
        // 创建新的Excel 工作簿
        Workbook workbook = new HSSFWorkbook();
​
        // 在Excel工作簿中建一工作表,其名为缺省值 Sheet0
        //Sheet sheet = workbook.createSheet();
​
        // 如要新建一名为"信息统计"的工作表,其语句为:
        Sheet sheet = workbook.createSheet("信息统计");
​
        // 创建行(row 1)
        Row row1 = sheet.createRow(0);
​
        // 创建单元格(col 1-1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日人数");
​
        // 创建单元格(col 1-2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);
​
        // 创建行(row 2)
        Row row2 = sheet.createRow(1);
​
        // 创建单元格(col 2-1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
​
        //创建单元格(第三列)
        Cell cell22 = row2.createCell(1);
        String dateTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(dateTime);
​
        // 新建一输出文件流(注意:要先创建文件夹)
        FileOutputStream out = new FileOutputStream("d://zx/a.xls");
        // 把相应的Excel 工作簿存盘
        workbook.write(out);
        // 操作结束,关闭文件
        out.close();
​
        System.out.println("文件生成成功");
    }
}

3.2.3 xlsx 文件写操作

excel2007+ 文件扩展名为 xlsx

代码语言:javascript
复制
package com.zx.poi;
​
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.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
​
import java.io.FileOutputStream;
import java.io.IOException;
​
public class Excel07Test {
​
    @Test
    public void testWrite07() throws IOException {
​
        // 创建新的Excel 工作簿
        Workbook workbook = new XSSFWorkbook();
​
        // 在Excel工作簿中建一工作表,其名为缺省值 Sheet0
        //Sheet sheet = workbook.createSheet();
​
        // 如要新建一名为"信息统计"的工作表,其语句为:
        Sheet sheet = workbook.createSheet("信息统计");
​
        // 创建行(row 1)
        Row row1 = sheet.createRow(0);
​
        // 创建单元格(col 1-1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日人数");
​
        // 创建单元格(col 1-2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);
​
        // 创建行(row 2)
        Row row2 = sheet.createRow(1);
​
        // 创建单元格(col 2-1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
​
        //创建单元格(第三列)
        Cell cell22 = row2.createCell(1);
        String dateTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(dateTime);
​
        // 新建一输出文件流(注意:要先创建文件夹)
        FileOutputStream out = new FileOutputStream("d://zx/b.xlsx");
        // 把相应的Excel 工作簿存盘
        workbook.write(out);
        // 操作结束,关闭文件
        out.close();
​
        System.out.println("文件生成成功");
    }
}

3.2.4 xls 文件读操作

代码语言:javascript
复制
// xls 2003 文件读操作
    @Test
    public void testXlsRead() throws Exception {
        //1 确定文件流
        FileInputStream is = new FileInputStream("D:\\xml\\user.xls");
        //2 开始读
        // 2.1 工作簿
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        // 2.2 工作表
        HSSFSheet sheet = workbook.getSheet("用户表");
        int rowStart = sheet.getFirstRowNum();      //第一行索引号(从0开始)
        int rowEnd = sheet.getLastRowNum();         //最后一行的索引号(从0开始)
        // 2.3 行
        for(int i = rowStart ; i <= rowEnd ; i ++) {
            HSSFRow row = sheet.getRow(i);
            int cellStart = row.getFirstCellNum();      //第一列的索引号(从0开始)
            int cellEnd = row.getLastCellNum() ;        //最后一列的编号(从1开始)
            // 2.4 列
            for(int c = cellStart; c < cellEnd ; c++) {
                HSSFCell cell = row.getCell(c);
//                System.out.println(cell.getCellType());
                if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    // 字符串
                    System.out.print(row.getCell(c).getStringCellValue() + ", ");
                }
                if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    // 字符串
                    System.out.print(row.getCell(c).getNumericCellValue() + ", ");
                }
            }
            System.out.println();
        }
        // 3 释放资源
        is.close();
    }

3.2.5 xlsx 文件读操作

代码语言:javascript
复制
 // xlsx 2007 文件读操作
    @Test
    public void testXlsxRead() throws Exception {
        //1 确定文件流
        FileInputStream is = new FileInputStream("D:\\xml\\user.xlsx");
        //2 开始读
        // 2.1 工作簿
        Workbook workbook = new XSSFWorkbook(is);
        // 2.2 工作表
        Sheet sheet = workbook.getSheet("用户表");
        int rowStart = sheet.getFirstRowNum();      //第一行索引号(从0开始)
        int rowEnd = sheet.getLastRowNum();         //最后一行的索引号(从0开始)
        // 2.3 行
        for(int i = rowStart ; i <= rowEnd ; i ++) {
            Row row = sheet.getRow(i);
            int cellStart = row.getFirstCellNum();      //第一列的索引号(从0开始)
            int cellEnd = row.getLastCellNum() ;        //最后一列的编号(从1开始)
            // 2.4 列
            for(int c = cellStart; c < cellEnd ; c++) {
                Cell cell = row.getCell(c);
//                System.out.println(cell.getCellType());
                if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    // 字符串
                    System.out.print(row.getCell(c).getStringCellValue() + ", ");
                }
                if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    // 字符串
                    System.out.print(row.getCell(c).getNumericCellValue() + ", ");
                }
            }
            System.out.println();
        }
        // 3 释放资源
        is.close();
    }

3.2.6 读取不同类型的数据

代码语言:javascript
复制
@Test
public void testRead07() throws Exception{
​
    InputStream is = new FileInputStream("d:/0704.xlsx");
​
    Workbook workbook = new XSSFWorkbook(is);
    Sheet sheet = workbook.getSheetAt(0);
​
    // 读取第一行第一列
    Row row = sheet.getRow(0);
    Cell cell1 = row.getCell(0);
    Cell cell2 = row.getCell(1);
​
​
    // 输出单元内容
    System.out.println(cell1.getStringCellValue());
    System.out.println(cell2.getNumericCellValue());
​
    // 操作结束,关闭文件
    is.close();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 3.POI入门(了解)
    • 3.1 POI 概述
      • 3.1.1 简介
      • 3.1.2 官网
    • 3.2 入门案例
      • 3.2.1 环境搭建 创建项目:
      • 3.2.2 xls文件写操作
      • 3.2.3 xlsx 文件写操作
      • 3.2.4 xls 文件读操作
      • 3.2.5 xlsx 文件读操作
      • 3.2.6 读取不同类型的数据
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档