遍历Excel第一列数据并添加单元格

在日常工作中,我们经常需要处理Excel表格中的数据。有时候,我们需要遍历Excel表格中的数据,并对其进行处理。本文将介绍如何使用Java程序遍历Excel中的第一列数据,并添加单元格的操作。

Excel文件读取

在Java中,我们可以使用Apache POI库来操作Excel文件。Apache POI是一个用于操作Microsoft文档格式的Java API。通过Apache POI,我们可以读取Excel文件中的内容,并对其进行操作。

首先,我们需要在pom.xml文件中添加Apache POI的依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.4</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

接下来,我们可以编写Java程序来读取Excel文件中的第一列数据:

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExcelReader {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream("data.xlsx");
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell cell = row.getCell(0);
                System.out.println(cell.getStringCellValue());
            }

            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

上面的代码片段演示了如何使用Apache POI来读取Excel文件中的第一列数据。我们首先打开Excel文件,然后获取第一个工作表,接着遍历工作表中的每一行,获取每行的第一个单元格数据,并输出到控制台。

添加单元格数据

除了读取Excel文件中的数据,有时候我们也需要向Excel文件中添加数据。下面是一个示例代码,演示了如何向Excel文件中添加数据到指定位置:

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriter {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream("data.xlsx");
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);

            Row row = sheet.createRow(1);
            Cell cell = row.createCell(0);
            cell.setCellValue("New Data");

            FileOutputStream fos = new FileOutputStream("data.xlsx");
            workbook.write(fos);

            workbook.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

上面的代码片段演示了如何使用Apache POI向Excel文件中添加数据。我们首先创建一个新行和新单元格,并向新单元格中添加数据,然后将更改写入Excel文件。

完整示例

下面是一个完整的示例,演示了如何遍历Excel表格中的第一列数据,并添加单元格:

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelProcessor {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream("data.xlsx");
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历Excel表格中的第一列数据
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell cell = row.getCell(0);
                System.out.println(cell.getStringCellValue());
            }

            // 添加数据到指定位置
            Row newRow = sheet.createRow(sheet.getLastRowNum() + 1);
            Cell newCell = newRow.createCell(0);
            newCell.setCellValue("New Data");

            // 将更改写入Excel文件
            FileOutputStream fos = new FileOutputStream("data.xlsx");
            workbook.write(fos);

            workbook.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

总结

本文介绍了如何使用Java程序遍历Excel表格中的第一列数据,并添加单元格的操作。通过Apache POI库,我们可以方便地读取和操作Excel文件中的数据。希望本文对您有所帮助!

参考文献

  • Apache POI官方网站: [