• POI入门


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

    💬文章目录

    💅1.1POI 概述

    💅 1.2

    💅 1.3官网

    💅2.门案例

    💅2.1境搭配 

    💅2.2xls文件写操作

    💅2.3xlsx文件写操作

    💅2.4xls文件读操作

    💅2.5xlsx文件读操作

    💅2.6读取不同类型的数据


                   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文件
      1. org.apache.poi
      2. poi
      3. 3.9
      4. org.apache.poi
      5. poi-ooxml
      6. 3.9
      7. joda-time
      8. joda-time
      9. 2.10.1
      10. junit
      11. junit
      12. 4.12

    • xls文件写操作


      • excel2003 文件扩展名为 xls

      • 名词:

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

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

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

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

    1. public class TestXls {
    2. //程序运行的根目录,classpath
    3. public String getPath(){
    4. return this.getClass().getResource("/").getPath();
    5. }
    6. @Test
    7. public void testWrite() throws IOException {
    8. //1.创建工作簿 Workbook
    9. Workbook workbook = new HSSFWorkbook();
    10. //2. 通过工作簿,创建工作表Sheet
    11. Sheet sheet = workbook.createSheet("表名");
    12. for (int i = 0; i < 5; i++) {
    13. //3.通过表,创建行 Row
    14. Row row = sheet.createRow(i);
    15. for (int j = 0; j < 10; j++) {
    16. //4. 通过行,创建单元格 Cell
    17. Cell cell = row.createCell(j);
    18. // 给单元格添加数据
    19. cell.setCellValue("测试"+i+":"+j);
    20. }
    21. }
    22. //5. 将工作簿以流的方式写入硬盘
    23. String file= getPath()+"demo01.xls";
    24. OutputStream out=new FileOutputStream(file);
    25. //6. 添加
    26. workbook.write(out);
    27. //7. 释放资源
    28. out.close();
    29. }
    30. }
    • xlsx文件写操作

    1. public class TestXlsx {
    2. //程序运行的根目录,classpath
    3. public String getPath(){
    4. return this.getClass().getResource("/").getPath();
    5. }
    6. @Test
    7. public void testWrite() throws IOException {
    8. //1.创建工作簿 Workbook
    9. Workbook workbook = new XSSFWorkbook();
    10. //2. 通过工作簿,创建工作表Sheet
    11. Sheet sheet = workbook.createSheet("Java12班");
    12. for (int i = 0; i < 5; i++) {
    13. //3.通过表,创建行 Row
    14. Row row = sheet.createRow(i);
    15. for (int j = 0; j < 10; j++) {
    16. //4. 通过行,创建单元格 Cell
    17. Cell cell = row.createCell(j);
    18. // 给单元格添加数据
    19. cell.setCellValue("测试"+i+":"+j);
    20. }
    21. }
    22. //5. 将工作簿以流的方式写入硬盘
    23. String file= getPath()+"demo01.xlsx";
    24. OutputStream out=new FileOutputStream(file);
    25. workbook.write(out);
    26. //释放资源
    27. out.close();
    28. }
    29. }

    • xls文件读操作

    1. @Test
    2. public void testRead() throws IOException {
    3. String file=getPath()+"demo01.xls";
    4. FileInputStream is = new FileInputStream(file);
    5. HSSFWorkbook workbook = new HSSFWorkbook(is);
    6. //获得工作表
    7. Sheet sheet=workbook.getSheet("java12班");
    8. //获得行
    9. int startRow = sheet.getFirstRowNum(); //第一列的索引号,从0开始
    10. int endRow = sheet.getLastRowNum(); //最后一列的索引号,从0开始
    11. for (int i = startRow; i
    12. Row row = sheet.getRow(i);
    13. short startCell = row.getFirstCellNum(); //第一列的索引号,从0开始
    14. short endCell = row.getLastCellNum(); //最后一列的索引号,但从1开始
    15. for (int j = startCell; j
    16. //4.获取列(单元格)
    17. Cell cell = row.getCell(j);
    18. //5.打印内容
    19. System.out.print(cell.getStringCellValue());
    20. System.out.print(",");
    21. }
    22. // 断点
    23. System.out.println();
    24. }
    25. }
    • xlsx文件读操作

    1. @Test
    2. public void testRead() throws IOException {
    3. String file=getPath()+"demo01.xlsx";
    4. FileInputStream is = new FileInputStream(file);
    5. Workbook workbook = new XSSFWorkbook(is);
    6. //获得工作表
    7. Sheet sheet=workbook.getSheet("java12班");
    8. //获得行
    9. int startRow = sheet.getFirstRowNum(); //第一列的索引号,从0开始
    10. int endRow = sheet.getLastRowNum(); //最后一列的索引号,从0开始
    11. for (int i = startRow; i
    12. Row row = sheet.getRow(i);
    13. short startCell = row.getFirstCellNum(); //第一列的索引号,从0开始
    14. short endCell = row.getLastCellNum(); //最后一列的索引号,但从1开始
    15. for (int j = startCell; j
    16. //4.获取列(单元格)
    17. Cell cell = row.getCell(j);
    18. //5.打印内容
    19. System.out.print(cell.getStringCellValue());
    20. System.out.print(",");
    21. }
    22. System.out.println();
    23. }
    24. }
    • 读取不同类型的数据

    1. @Test
    2. public void testRead07() throws Exception{
    3. InputStream is = new FileInputStream("d:/0704.xlsx");
    4. Workbook workbook = new XSSFWorkbook(is);
    5. Sheet sheet = workbook.getSheetAt(0);
    6. // 读取第一行第一列
    7. Row row = sheet.getRow(0);
    8. Cell cell1 = row.getCell(0);
    9. Cell cell2 = row.getCell(1);
    10. // 输出单元内容
    11. System.out.println(cell1.getStringCellValue());
    12. System.out.println(cell2.getNumericCellValue());
    13. // 操作结束,关闭文件
    14. is.close();
    15. }

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

                                                                            


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

  • 相关阅读:
    $概率DP$
    Postman设置全局变量和传参
    web前端期末大作业——基于HTML+CSS+JavaScript蓝色的远程监控设备系统后台管理界面模板
    java+springboot+vue高校毕业生就业统计管理系统022xr
    学习STM32第十八天
    计算数组中各元素的平方numpy.square()
    Vue.js模板语法概述
    C++学习day--23 枚举、类型定义、头文件
    Redis入门 (店铺营业状态设置) --苍穹外卖day4
    18.2 使用NPCAP库抓取数据包
  • 原文地址:https://blog.csdn.net/lcshen1234/article/details/126745604