`我正在尝试读取一个Excel文件。我不确定我在这里缺少什么。但是我在下面出现错误。我的变量书还显示,即使在下一行中也从未使用过此变量。我认为它以某种方式相关,但不确定如何。
任何帮助是极大的赞赏。

错误:无法初始化主类myExcelProject.MyExcelFileReader
造成原因:java.lang.NoClassDefFoundError:org / apache / poi / ss / usermodel / Workbook
`

package myExcelProject;

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

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class MyExcelFileReader {
    private String[][] inputs;
    private static String[][] excelInputs;
    private static Row row;
    private static Cell cell;

    public static void main(String[] args) {
        try {
                excelInputs=readExcelTable("Excel Report.xlsx");
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public static String[][] readExcelTable(String excelFile) throws IOException{

        FileInputStream inputStream = new FileInputStream(new File(excelFile));
        Workbook book = new XSSFWorkbook(inputStream);
        Sheet firstSheet = book.getSheetAt(0);
        int lastRowNum = firstSheet.getLastRowNum();

        String[][] excelReadout = new String[lastRowNum][4];
        for (int i = 3; i < lastRowNum; i++) {
            row = firstSheet.getRow(i);
            if (row == null) {
            } else {
                for (int j = 0; j < 4; j++) {
                    cell = row.getCell(j, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
                    if (cell == null) {
                        // do nothing
                    } else {
                        excelReadout[i][j]=cell.toString();
                        //System.out.print(cell.toString());
                    }
                }
                System.out.println();
            }
        }
        return excelReadout;
    }

}

最佳答案

将jar从Modulepath移到了Eclipse中的Classpaths部分。解决了问题。

10-04 11:51
查看更多