java - 尝试使用提供的代码读取 Excel 文件时出现 Class Not Found 错误
问题描述
尝试读取 Excel 文件时出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
我已使用以下代码读取扩展名为 .xls 的 Excel 文件。我曾尝试通过导入其他 poi jar 来搜索答案,但它没有帮助,我已经检查过搜索网络,但它再次没有帮助。
public Object[][] getExcelData(String excelLocation, String sheetName) {
try {
Object dataSets[][] = null;
FileInputStream file = new FileInputStream(new File(excelLocation));
HSSFWorkbook workbook = new HSSFWorkbook(excelLocation);
HSSFSheet sheet = workbook.getSheet(sheetName);
int totalRowNum = sheet.getLastRowNum();
int totalColumnNum = sheet.getRow(0).getLastCellNum();
dataSets = new Object[totalRowNum][totalColumnNum];
Iterator<Row> rowIterator = sheet.iterator();
int i = 0;
while (rowIterator.hasNext()) {
i++;
HSSFRow row = (HSSFRow) rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
int j = 0;
while (cellIterator.hasNext()) {
j++;
HSSFCell cell = (HSSFCell) cellIterator.next();
switch (cell.getCellTypeEnum()) {
case STRING:
dataSets[i][j] = cell.getStringCellValue();
break;
case NUMERIC:
dataSets[i][j] = cell.getNumericCellValue();
break;
case BOOLEAN:
dataSets[i][j] = cell.getBooleanCellValue();
break;
case FORMULA:
dataSets[i][j] = cell.getCellFormula();
break;
default:
log.info("No matching ENUM Type Found");
break;
}
}
}
return dataSets;
} catch (Exception e) {
log.info(e.getCause());
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ExcelHelper excel = new ExcelHelper();
String excelLocation = ResourceHelper.getResourcePath("\\src\\main\\resources\\testData\\testData.xls");
Object[][] data = excel.getExcelData(excelLocation, "Login");
}
}
解决方案
从您的用户位置删除 .m2 -> 执行 Maven 强制更新 -> 运行
推荐阅读
- javascript - Chartist 未在 Nuxt 的阈值插件中定义
- java - 单元测试因 NullPoiterException 而失败
- windows - 如何使用 Cmdkey.exe /list powershell 脚本报告 Office 应用程序中的 microsoft 帐户许可证?
- azure-logic-apps - 如何在表达式中使用整数运算?
- heroku - 是否可以将 Heroku 日志排放设置为仅“排放”应用程序日志?
- python - Discord Py,通过 ID 在任何通道中发送消息
- r - 如何在具有多个条件的 R 中应用 RLE() 函数?
- flutter - 在设备启动时运行后台服务
- c - 3 x 3 网格的阿贝尔沙堆的特征是什么?
- docker - docker:如何在 shell 入口点“sh -c”中使用 shell 变量?