首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >包org.apache.poi.xssf.usermodel不存在

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

Stack Overflow用户
提问于 2022-10-03 11:40:46
回答 1查看 286关注 0票数 -1

我有一个存储火车座位预订系统数据的应用程序。

我使用apache将数据读写到excel文件中。Apache版本: 5.2.2,5.2.3 (从此链接下载)

代码语言:javascript
运行
复制
//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":中使用,但是当我按照case "L":的想法执行这段代码(默认的IntelliJ )时,我会得到这个错误。):包org.apache.poi.xssf.usermodel不存在

我不知道是怎么发生的。但是我的原始代码并没有给出这个错误。(从这个意义上说,我在一个单独的文件夹中开发了这段代码,后来又将它们分解为子文件夹,因为我的任务要求我这样做)

我以前有这个问题,用更新的代码替换了旧文件夹,但是这个错误仍然发生。

我不能包括这张照片,因为我没有足够的声誉。

帮我做这件事,展示一些爱:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-04 08:53:10

所以问题是依赖关系。我还没有正确地将所需的依赖项添加到我希望依赖项工作的项目中。

如何添加依赖项(IntelliJ Idea)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73934907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档