首先,对不起基本问题。但是,我无法正常运行。整个代码都是这样的,我用eclipse运行它。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.*;
public class hannimpeha1 {
public static void main(String[] args) throws IOException {
// Locate xls file.
File workbookfile = new File("C:\\Users\\bok\\Desktop\\old.xls");
FileInputStream file = new FileInputStream(workbookfile);
POIFSFileSystem fs = new POIFSFileSystem(file);
@SuppressWarnings("resource")
HSSFSheet sheet = new HSSFWorkbook(fs).getSheetAt(1);
// Get row and column count.
int rowCount = sheet.getPhysicalNumberOfRows();
int colCount = sheet.getRow(0).getLastCellNum();
// Iterate over rows and columns.
for(int r = 0; r < rowCount; r++) {
HSSFRow row = sheet.getRow(r);
for(int c = 0; c < colCount; c++) {
HSSFCell cell = row.getCell(c);
String cellval = cell.toString();
System.out.print(" | " + cellval);
}
System.out.println(" |");
}
}
}
代码本身似乎没问题,但它抛弃了,
"Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at hannimpeha1.main(hannimpeha1.java:15)"
如果我将XSSFRow row = null;
设置为禁止显示错误消息,否则我会The local variable row may not have been initialized
答案 0 :(得分:0)
在构建路径中添加commons-collections4-x.x.jar文件并再次尝试。它会起作用。
答案 1 :(得分:0)
必须使用方法中的值初始化局部变量,否则在方法(实例变量)之外声明变量。
以下是下载jar文件的链接。
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.9