/*

  • Apache POI Excel 导入导出简单 GUI 示例,带启动画面
    */

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

import javax.swing.; import java.awt.;
import java.awt.event.ActionEvent;
import java.io.*;

public class ExcelGUI extends JFrame {

public ExcelGUI() {
    setTitle("Excel 导入导出示例");
    setSize(400, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    JButton exportBtn = new JButton("导出 Excel");
    JButton importBtn = new JButton("导入 Excel");

    exportBtn.addActionListener(this::exportExcel);
    importBtn.addActionListener(this::importExcel);

    JPanel panel = new JPanel();
    panel.add(exportBtn);
    panel.add(importBtn);

    add(panel);
}

private void exportExcel(ActionEvent e) {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("用户信息");

    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("编号");
    header.createCell(1).setCellValue("姓名");
    header.createCell(2).setCellValue("年龄");

    Object[][] userData = {
        {1, "张三", 25},
        {2, "李四", 30},
        {3, "王五", 28}
    };

    int rowNum = 1;
    for (Object[] data : userData) {
        Row row = sheet.createRow(rowNum++);
        for (int i = 0; i < data.length; i++) {
            row.createCell(i).setCellValue(data[i].toString());
        }
    }

    try (FileOutputStream fos = new FileOutputStream("用户信息.xlsx")) {
        workbook.write(fos);
        JOptionPane.showMessageDialog(this, "导出成功: 用户信息.xlsx");
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "导出失败: " + ex.getMessage());
    }
}

private void importExcel(ActionEvent e) {
    try (FileInputStream fis = new FileInputStream("用户信息.xlsx");
         Workbook workbook = new XSSFWorkbook(fis)) {

        Sheet sheet = workbook.getSheetAt(0);
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            int id = (int) row.getCell(0).getNumericCellValue();
            String name = row.getCell(1).getStringCellValue();
            int age = (int) row.getCell(2).getNumericCellValue();
            sb.append(String.format("编号: %d, 姓名: %s, 年龄: %d\n", id, name, age));
        }

        JOptionPane.showMessageDialog(this, sb.toString());
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "导入失败: " + ex.getMessage());
    }
}

private static void showSplash() {
    JWindow splash = new JWindow();
    JLabel label = new JLabel("正在启动 Excel 应用...", SwingConstants.CENTER);
    label.setFont(new Font("SansSerif", Font.BOLD, 20));
    splash.getContentPane().add(label);
    splash.setSize(300, 150);
    splash.setLocationRelativeTo(null);
    splash.setVisible(true);

    try {
        Thread.sleep(2000); // 显示2秒
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    splash.setVisible(false);
    splash.dispose();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        showSplash();
        new ExcelGUI().setVisible(true);
    });
}

}

✅ 已添加启动画面功能,特点如下:

  • 程序启动时弹出 JWindow 显示“正在启动 Excel 应用…”文字
  • 启动画面持续 2 秒后自动关闭,进入主界面

如需美化启动界面(如添加图片、渐隐动画等),也可以继续扩展,告诉我即可。