POI入门

发布于:2022-12-25 ⋅ 阅读:(557) ⋅ 点赞:(0)
  • 💂 个人主页: 程序员爱摸鱼
  • 🤟 版权: 本文由【程序员爱摸鱼】原创、在CSDN首发、需要转载请联系博主
  • 💬 如果文章对你有帮助、欢迎关注+点赞+收藏(一键三连)哦
  • 💅 想寻找共同成长的小伙伴,可以互粉哦

💬文章目录

💅1.1POI 概述

💅 1.2

💅 1.3官网

💅2.门案例

💅2.1境搭配 

💅2.2xls文件写操作

💅2.3xlsx文件写操作

💅2.4xls文件读操作

💅2.5xlsx文件读操作

💅2.6读取不同类型的数据


  • POI 概述

               Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”

功能 描述
HSSFWorkBook 提供读写Microsoft Excel格式档案的功能,xls文档
XSSFWorkBook 提供读写Microsoft Excel OOXML格式档案的功能,xlsx文件
HWPF 提供读写Microsoft Word格式档案的功能
HSLF 提供读写Microsoft PowerPoint格式档案的功能
HDGF 提供读写Microsoft Visio格式档案的功能

HPBF  

提供读Microsoft Publisher格式档案的功能
HSMF  提供读Microsoft Outlook格式档案的功能

  •  官网

Apache POI - the Java API for Microsoft Documents

  • 入门案例

    • 坏境搭配 

      • 创建项目zx-test-parent

  • 配置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>

  • xls文件写操作


    • excel2003 文件扩展名为 xls

    • 名词:

      • 工作簿:一个excel文件,就是一个工作簿

      • 工作表:一个工作簿中,可以所有多个工作表Sheet

      • 行:每一个工作表,包含多行row

      • 单元格:每行有多个单元格Cell组成。

public class TestXls {

    //程序运行的根目录,classpath
    public String getPath(){
        return  this.getClass().getResource("/").getPath();
    }

    @Test
    public void testWrite() throws IOException {
        //1.创建工作簿 Workbook
        Workbook workbook = new HSSFWorkbook();
        //2. 通过工作簿,创建工作表Sheet
        Sheet sheet = workbook.createSheet("表名");
        for (int i = 0; i < 5; i++) {
            //3.通过表,创建行 Row
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                //4. 通过行,创建单元格 Cell
                Cell cell = row.createCell(j);
                // 给单元格添加数据
                cell.setCellValue("测试"+i+":"+j);
            }
        }
        //5. 将工作簿以流的方式写入硬盘
        String file= getPath()+"demo01.xls";
        OutputStream out=new FileOutputStream(file);
        //6. 添加
        workbook.write(out);
        //7. 释放资源
        out.close();
    }
}
  • xlsx文件写操作

public class TestXlsx {

    //程序运行的根目录,classpath
    public String getPath(){
        return  this.getClass().getResource("/").getPath();
    }

    @Test
    public void testWrite() throws IOException {
        //1.创建工作簿 Workbook
        Workbook workbook = new XSSFWorkbook();
        //2. 通过工作簿,创建工作表Sheet
        Sheet sheet = workbook.createSheet("Java12班");
        for (int i = 0; i < 5; i++) {
            //3.通过表,创建行 Row
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                //4. 通过行,创建单元格 Cell
                Cell cell = row.createCell(j);
                // 给单元格添加数据
                cell.setCellValue("测试"+i+":"+j);
            }
        }
        //5. 将工作簿以流的方式写入硬盘
        String file= getPath()+"demo01.xlsx";
        OutputStream out=new FileOutputStream(file);
        workbook.write(out);
         //释放资源
        out.close();
    }
}

  • xls文件读操作

@Test
    public void testRead() throws IOException {
        String file=getPath()+"demo01.xls";
        FileInputStream is = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(is);

        //获得工作表
        Sheet sheet=workbook.getSheet("java12班");

        //获得行
        int startRow = sheet.getFirstRowNum();   //第一列的索引号,从0开始
        int endRow = sheet.getLastRowNum();      //最后一列的索引号,从0开始

        for (int i = startRow; i <endRow ; i++) {
            Row row = sheet.getRow(i);

            short startCell = row.getFirstCellNum();    //第一列的索引号,从0开始
            short endCell = row.getLastCellNum();       //最后一列的索引号,但从1开始

            for (int j = startCell; j <endCell ; j++) {
                //4.获取列(单元格)
                Cell cell = row.getCell(j);
                //5.打印内容
                System.out.print(cell.getStringCellValue());
                System.out.print(",");
            }
            // 断点
            System.out.println();
        }
    }
  • xlsx文件读操作

 @Test
    public void testRead() throws IOException {
        String file=getPath()+"demo01.xlsx";
        FileInputStream is = new FileInputStream(file);
        Workbook workbook = new XSSFWorkbook(is);

        //获得工作表
        Sheet sheet=workbook.getSheet("java12班");

        //获得行
        int startRow = sheet.getFirstRowNum();   //第一列的索引号,从0开始
        int endRow = sheet.getLastRowNum();      //最后一列的索引号,从0开始

        for (int i = startRow; i <endRow ; i++) {
            Row row = sheet.getRow(i);

            short startCell = row.getFirstCellNum();    //第一列的索引号,从0开始
            short endCell = row.getLastCellNum();       //最后一列的索引号,但从1开始

            for (int j = startCell; j <endCell ; j++) {
                //4.获取列(单元格)
                Cell cell = row.getCell(j);
                //5.打印内容
                System.out.print(cell.getStringCellValue());
                System.out.print(",");
            }
            System.out.println();
        }
    }
  • 读取不同类型的数据

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

到这里说明你已经学会了哦,努力学习!学无止境!!!

                                                                        


想要了解更多吗?没时间解释了,快来点一点!!!
程序员爱摸鱼🐟
————————————————
版权声明:本文为CSDN博主「程序员爱摸鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:程 序 员 爱 摸 鱼🐟

本文含有隐藏内容,请 开通VIP 后查看