包 org.apache.poi.xssf.usermodel 不存在

问题描述 投票:0回答:1

我有一个存储火车座位预订系统数据的应用程序。
我正在使用 apache poi 读取数据并将其写入 Excel 文件。 Apache-poi 版本:5.2.2、5.2.3(从此链接下载)

//import org.apache.commons.lang3.ArrayUtils;
//package org.apache.commons.lang3;
package com.company;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Train
{
    public static void main(String[] args) throws IOException {
        int coachNumber = 0;
        String roomName = null; // This will be the coach assigned to the customer
        String choice = "";
        String deleteCustomer;
        int foundCustomerIndex = 0;

        String[] customer = new String[11];
        Scanner input = new Scanner(System.in);

        //initialise
        initialise(customer); //better to initialise in a procedure


            while (!choice.equals("Q")) {
                System.out.println("\n===== Menu =====" +
                        "\nA - Add Customers\n" +
                        "V - View All Coaches\n" +
                        "E - Display Empty Coaches\n" +
                        "D - Delete Customer From Coach\n" +
                        "F - Find Coach From Customer name\n" +
                        "S - Store program data into file\n" +
                        "L - Load program data from file\n" +
                        "O - View guests Ordered alphabetically by name\n" +
                        "Q - To exit\n" +
                        "================\n");

                System.out.print("\nEnter a letter from above to proceed: ");

                try {
                    choice = input.next();
                }
                catch (Exception e) {
                    System.out.println("Exception Handled.\nEnter valid input");
                    System.out.print("\nEnter a letter from above to proceed: ");
                    choice = input.next();
                }

                    switch (choice.toUpperCase(Locale.ROOT)) {

// Case A -->
                        //Add Customer
                    case "A":
                        System.out.println("\n===== Add Customer =====\n");
                        System.out.println("Enter coach number (0-9) or 10 to stop:");
                        coachNumber = input.nextInt();

                        System.out.println("Enter your name for coach " + coachNumber + " :");
                        roomName = input.next();
                        customer[coachNumber] = roomName;

                        break;

// case V
                        // View All coaches
                        case "V":
                            System.out.println("\n===== View All coaches =====\n");


                        for (int x = 0; x < 10; x++) {
                            System.out.println("coach " + x + " occupied by " + customer[x]);
                        }
                        break;

// Case E -->
                    // Display Empty coaches
                    case "E":
                        System.out.println("\n===== Empty Coaches =====\n");

                        for (int x = 0; x < 10; x++) {
                            if (customer[x].equals("e")) {
                                System.out.println("coach " + x + " is empty");
                            }
                        }
                        break;

// Case D -->
                    // Delete a Customer from Coach
                    case "D":

                        System.out.println("\n===== Delete a Customer from Coach =====\n");

                        System.out.println("Enter customer name to delete: ");
                        deleteCustomer = input.next();

                        for (int i = 0; i < 10; i++) {
                            if (customer[i] == "e")
                                continue;
                            else {
                                customer[i] = "e";
                                roomName = "e";
                                System.out.println("\nCustomer " + deleteCustomer + " has been removed!");
                                break;
                            }
                        }
                        break;

// Case F -->
                    // Find Coach from customer name
                    case "F":
                        System.out.println("\n===== Find Coach from customer name =====\n");

                        String findCustomerName;
                        System.out.println("Find Customer Name: ");
                        findCustomerName = input.next();

                        for (int i = 0; i < customer.length; i++) {
                            if (findCustomerName.equals(customer[i])) {
                                System.out.println("Customer Found.\nCabin Number: " + i);
                                break;
                            }
                        }
                        break;

// Case S -->
                    // Store/ Write program data into file
                    case "S":
                        System.out.println("\n===== Store program data into file =====\n");
                        XSSFWorkbook workbook = new XSSFWorkbook(); //Empty workbook
                        XSSFSheet sheet = workbook.createSheet("Emp Info"); //Empty sheet

                        // Adding data to the sheet
                        Object empdata[][] = {
                                {"coachNo.", "name"}, //Header
                                {coachNumber, roomName},

                        };

                        // Using normal loop
                        int rows = empdata.length;
                        int cols = empdata[0].length;
//                    System.out.println(rows); //4 rows
//                    System.out.println(cols); //3 columns

                            for (int r = 0; r < rows; r++) //rows
                        {
                            XSSFRow row = sheet.createRow(r); //Create new row

                            for (int c = 0; c < cols; c++) //cells
                            {
                                XSSFCell cell = row.createCell(c); //Creating cell
                                Object value = empdata[r][c]; //cell location

                                if (value instanceof String)
                                    cell.setCellValue((String) value);
                                if (value instanceof Integer)
                                    cell.setCellValue((Integer) value);
                                if (value instanceof Boolean)
                                    cell.setCellValue((Boolean) value);
                            }
                        }

                        String filePath = ".\\writeCustomers.xlsx"; //Save location for the excel file
                        FileOutputStream outstream = new FileOutputStream(filePath);
                        workbook.write(outstream);

                        outstream.close();

                        System.out.println("\nFile has been written successfully.\nFilename: writeCustomers.xlsx ...");
                        break;

// Case L -->
                    // Load/ Read program data from file
                    case "L":
                        System.out.println("\n===== Load program data from file =====\n");

                        String excelFilePath = ".\\writeCustomers.xlsx"; //Read file location
                        FileInputStream inputStream = new FileInputStream(excelFilePath); //File opens in read mode

                        XSSFWorkbook readWorkbook = new XSSFWorkbook(inputStream); //Getting workbook from poi

                        XSSFSheet readSheet = readWorkbook.getSheet("Emp Info"); // Sheet Name


                        // With iterators

                        Iterator iterator = readSheet.iterator(); // To capture all the data into iterator


                        while (iterator.hasNext()) // Checking the iterator has the next record
                        {
                            XSSFRow row = (XSSFRow) iterator.next(); //Row iterator
                            Iterator cellIterator = row.cellIterator(); //iterate all the cells

                            while (cellIterator.hasNext()) //Checking cell present or not
                            {
                                XSSFCell cell = (XSSFCell) cellIterator.next();

                                switch (cell.getCellType()) //Type of the cell
                                {
                                    case STRING: //if cell type string
                                        System.out.print(cell.getRichStringCellValue());
                                        break;

                                    case NUMERIC:
                                        System.out.print(cell.getNumericCellValue());
                                        break;

                                    case BOOLEAN:
                                        System.out.print(cell.getBooleanCellValue());
                                        break;
                                }
                                System.out.print("  |  ");
                            }
                            System.out.println();
                        }
                        break;

// Case O -->
                    // View guest name in alphabetic order
                    case "O":
                        System.out.println("\n===== View guest name in alphabetic order =====\n");

                        //customer is the array name
                        for (int i = 0; i < customer.length - 1; i++) {
                            for (int j = i + 1; j < customer.length; j++) {
                                if (customer[i].compareTo(customer[j]) > 0) {
                                    String temp = customer[i];
                                    customer[i] = customer[j];
                                    customer[j] = temp;
                                }
                            }
                        }
                        System.out.println(Arrays.toString(customer));
                        break;

// Case Q -->
                    // Exit
                    case "Q":
                        System.out.println("\nYou have exitted the program.");
                        break;
                }
            }
        }





private static String[] sortArray(String[] customer)
{
    for (int i = 0; i<10; i++)
    {
        for(int j = i + 1; j > 0; j--)
        {
            if(customer[j].compareTo(customer[j-1]) < 0)
            {
                String temp = customer[j];
                customer[j] = customer[j-1];
                customer[j-1] = temp;
            }
        }
    }
    return customer;
}


// to view all coaches with e [empty]
private static void initialise(String hotelRef[]) {
    for (int x = 0; x < 10; x++) hotelRef[x] = "e";
    System.out.println("initilise ");
    }
}

Excel文件用于

 case "S":
 case "L":
但是当我在我的 IntelliJ idea(默认 IDE。更新到最新)上执行此代码时,我收到此错误: package org.apache.poi.xssf.usermodel 不存在
我不知道这是如何发生的。但我原来的代码并没有报这个错误。 (原创是指我在一个单独的文件夹中开发了这段代码,后来将它们分成了子文件夹,因为我的任务要求我这样做)
我之前遇到过这个问题,并用更新的代码替换了旧文件夹,但仍然出现此错误。
我无法在其中包含照片,因为我没有足够的照片 声誉。
帮助我并表达一些爱:)

java excel apache-poi
1个回答
0
投票

所以问题出在依赖关系上。我尚未将所需的依赖项正确添加到我希望依赖项起作用的项目中。

如何添加依赖(IntelliJ Idea)

文件 -> 项目结构 -> 模块 -> 依赖项

© www.soinside.com 2019 - 2024. All rights reserved.