【在线教育】POI入门

文章目录

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

    <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组成。

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

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 文件读操作

// 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 文件读操作

 // 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 读取不同类型的数据

@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();
}

posted @ 2022-11-09 08:11  陶然同学  阅读(60)  评论(0编辑  收藏  举报