读取桌面的Excel文件并解析数据

在实际开发中,经常需要读取Excel文件中的数据进行处理。本文将介绍如何使用Java代码读取桌面上的Excel文件,并解析其中的数据。

准备工作

首先,需要引入Apache POI库来处理Excel文件。你可以在项目中加入以下依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

代码实现

下面是一个简单的Java代码示例,用于读取桌面上的Excel文件并将数据打印出来:

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {

    public static void main(String[] args) {
        String filePath = System.getProperty("user.home") + "/Desktop/example.xlsx";
        try {
            FileInputStream file = new FileInputStream(new File(filePath));
            Workbook workbook = WorkbookFactory.create(file);

            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        default:
                            System.out.print("Unknown type\t");
                    }
                }
                System.out.println();
            }

            file.close();
            workbook.close();
        } catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
            e.printStackTrace();
        }
    }
}

以上代码会读取桌面上名为example.xlsx的Excel文件,并将数据输出到控制台。

实际问题

我们假设我们有一个Excel文件,包含员工的姓名、年龄和工资信息,我们希望读取这个文件并进行进一步的数据处理。

数据结构设计

下面是员工信息的数据结构设计:

erDiagram
    EMPLOYEE {
        string Name
        int Age
        int Salary
    }

数据处理

我们可以根据上面的代码,将读取到的数据存储到一个List<Employee>中:

import java.util.ArrayList;
import java.util.List;

public class Employee {
    private String name;
    private int age;
    private int salary;

    public Employee(String name, int age, int salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    // getters and setters
}

public class ExcelReader {

    public static void main(String[] args) {
        String filePath = System.getProperty("user.home") + "/Desktop/example.xlsx";
        List<Employee> employees = new ArrayList<>();
        try {
            FileInputStream file = new FileInputStream(new File(filePath));
            Workbook workbook = WorkbookFactory.create(file);

            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                String name = row.getCell(0).getStringCellValue();
                int age = (int) row.getCell(1).getNumericCellValue();
                int salary = (int) row.getCell(2).getNumericCellValue();
                employees.add(new Employee(name, age, salary));
            }

            file.close();
            workbook.close();
        } catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
            e.printStackTrace();
        }

        // 进行进一步的数据处理
        for (Employee employee : employees) {
            System.out.println(employee.getName() + " " + employee.getAge() + " " + employee.getSalary());
        }
    }
}

结论

通过以上代码示例,我们可以轻松地读取桌面上的Excel文件,并解析其中的数据进行进一步的处理。这对于处理Excel数据来说非常有用,希望本文对你有所帮助。