Apache POI 处理excel文件 记录用法

Apache POI

写excel

public static void write() throws IOException {
   
        //再内存中创建了一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();

        //创建一个sheet页
        XSSFSheet sheet = excel.createSheet("info");

        //这里创建行对象,这里的rownum 是从0开始的,类似于数组
        XSSFRow row = sheet.createRow(1);

        //创建单元格 这里的index也是从0开始的
        row.createCell(1).setCellValue("姓名"); //创建单元格,并且设置单元格内容
        row.createCell(3).setCellValue("城市");

        //新起一行
        row = sheet.createRow(2);
        row.createCell(1).setCellValue("jjking");
        row.createCell(3).setCellValue("广州");

        FileOutputStream fos = new FileOutputStream(new File("info.xlsx"));
        excel.write(fos);


        //关闭资源
        fos.close();
        excel.close();
    }

这个也不用过多的介绍,我们只要照着写就ok了,没什么难的

这样子,过后写好的excel,是这样的
在这里插入图片描述

读excel

public static void read() throws IOException, InvalidFormatException {
   
   XSSFWorkbook excel = new XSSFWorkbook(new File("info.xlsx"));

   //读取sheet页
   XSSFSheet sheet = excel.getSheetAt(0);

   //读取最后一个有文字的行号
   int lastRowNum = sheet.getLastRowNum();


   //从第2行开始读
   for(int i = 1; i <= lastRowNum; i++) {
   
       //获取行
       XSSFRow row = sheet.getRow(i);

       //获取单元格的对象
       String cellValue1 = row.getCell(1).getStringCellValue();
       String cellValue2 = row.getCell(3).getStringCellValue();

       System.out.println(cellValue1 + " " + cellValue2);
   }


   excel.close();
}

在这里插入图片描述

相关推荐

  1. Excel 数据处理记录

    2024-01-31 06:54:03       51 阅读
  2. Lombok 记录各种

    2024-01-31 06:54:03       57 阅读
  3. 记录 | python isinstance()

    2024-01-31 06:54:03       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-31 06:54:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-31 06:54:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-31 06:54:03       82 阅读
  4. Python语言-面向对象

    2024-01-31 06:54:03       91 阅读

热门阅读

  1. 【技术预研】StarRocks官方文档浅析(3)

    2024-01-31 06:54:03       82 阅读
  2. 【Spark系列6】如何做SQL查询优化和执行计划分析

    2024-01-31 06:54:03       46 阅读
  3. flink分别使用FilterMap和ProcessFunction实现去重逻辑

    2024-01-31 06:54:03       55 阅读
  4. 双非本科准备秋招(11.2)—— 力扣字符串

    2024-01-31 06:54:03       62 阅读
  5. 设计模式七(策略模式)

    2024-01-31 06:54:03       62 阅读
  6. 深入理解c语言printf

    2024-01-31 06:54:03       51 阅读
  7. 分布式场景怎么Join

    2024-01-31 06:54:03       58 阅读