EXCEL 下载 java

在开发过程中,经常会遇到需要从数据库中获取数据然后导出到Excel表格中的需求。而在Java开发中,我们可以通过使用Apache POI库来实现生成Excel文件。本文将介绍如何使用Java代码来实现从数据库中获取数据并下载生成Excel文件。

准备工作

在开始之前,我们需要先引入Apache POI库到我们的项目中。如果使用Maven进行项目管理,可以在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.4</version>
</dependency>

数据准备

在这里,我们假设我们有一个名为Employee的表,包含了员工的信息,字段包括idnamedepartmentsalary。我们将从数据库中查询这些信息并将其保存到Excel文件中。

代码实现

首先,我们需要编写一个方法来查询数据库获取数据。这里我们使用一个简单的伪代码来代替数据库查询过程:

public List<Employee> getEmployees() {
    List<Employee> employees = new ArrayList<>();
    
    // 假设这里是数据库查询过程
    employees.add(new Employee(1, "Alice", "IT", 5000));
    employees.add(new Employee(2, "Bob", "HR", 4500));
    employees.add(new Employee(3, "Charlie", "Finance", 6000));
    
    return employees;
}

然后,我们编写生成Excel文件的方法:

public void downloadExcel() {
    List<Employee> employees = getEmployees();
    
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Employee Data");
    
    int rowNum = 0;
    for (Employee employee : employees) {
        Row row = sheet.createRow(rowNum++);
        row.createCell(0).setCellValue(employee.getId());
        row.createCell(1).setCellValue(employee.getName());
        row.createCell(2).setCellValue(employee.getDepartment());
        row.createCell(3).setCellValue(employee.getSalary());
    }
    
    try (FileOutputStream outputStream = new FileOutputStream("employee_data.xlsx")) {
        workbook.write(outputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在上面的代码中,我们首先获取员工信息列表,然后创建一个XSSFWorkbook对象,接着创建一个XSSFSheet对象来存储数据。然后遍历员工信息列表,将每个员工的信息写入Excel文件中。

最后,我们使用FileOutputStream将数据写入到名为employee_data.xlsx的Excel文件中。

完整代码

下面是完整的Java代码示例:

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

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelDownloader {

    public static void main(String[] args) {
        downloadExcel();
    }

    public static List<Employee> getEmployees() {
        List<Employee> employees = new ArrayList<>();

        // 假设这里是数据库查询过程
        employees.add(new Employee(1, "Alice", "IT", 5000));
        employees.add(new Employee(2, "Bob", "HR", 4500));
        employees.add(new Employee(3, "Charlie", "Finance", 6000));

        return employees;
    }

    public static void downloadExcel() {
        List<Employee> employees = getEmployees();

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Employee Data");

        int rowNum = 0;
        for (Employee employee : employees) {
            XSSFRow row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getDepartment());
            row.createCell(3).setCellValue(employee.getSalary());
        }

        try (FileOutputStream outputStream = new FileOutputStream("employee_data.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class Employee {
        private int id;
        private String name;
        private String department;
        private int salary;

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

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getDepartment() {
            return department;