当我尝试读取Excel文件时,我收到以下异常:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.apache.poi.openxml4j.opc.PackageRelationshipCollection
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:304)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:156)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:124)
at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:112)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:83)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:128)
at org.apache.poi.openxml4j.opc.ZipPackagePart.<init>(ZipPackagePart.java:78)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:218)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:223)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
at readAndWriteToExcel.ReadingExcel.main(ReadingExcel.java:36)
我的代码是:
package readAndWriteToExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
public class ReadingExcel {
private static final Log LOG = LogFactory
.getLog(ReadingExcel.class);
public static void main(String[] args) throws Exception {
String SAMPLE_PERSON_DATA_FILE_PATH = "C:/Users/Documents/Test Data 5.xlsx";
File file = new File(SAMPLE_PERSON_DATA_FILE_PATH);
InputStream inputStream = new FileInputStream(file);
// The package open is instantaneous, as it should be.
OPCPackage pkg = null;
try {
ExcelWorkSheetRowCallbackHandler sheetRowCallbackHandler = new ExcelWorkSheetRowCallbackHandler(
new ExcelRowContentCallback() {
public void processRow(int rowNum, Map<String, String> map) {
// Do any custom row processing here, such as save
// to database
// Convert map values, as necessary, to dates or
// parse as currency, etc
System.out.println("rowNum=" + rowNum + ", map=" + map);
}
});
pkg = OPCPackage.open(inputStream);
ExcelSheetCallBack sheetCallback = new ExcelSheetCallBack() {
private int sheetNumber = 0;
public void startSheet(int sheetNum) {
this.sheetNumber = sheetNum;
System.out.println("Started processing sheet number=" + sheetNumber);
}
public void endSheet() {
System.out.println("Processing completed for sheet number=" + sheetNumber);
}
};
System.out.println("Constructor: pkg, sheetRowCallbackHandler, sheetCallback");
ExcelReader example1 = new ExcelReader(pkg,
sheetRowCallbackHandler, sheetCallback);
example1.process();
System.out.println("nConstructor: filePath, sheetRowCallbackHandler, sheetCallback");
ExcelReader example2 = new ExcelReader(SAMPLE_PERSON_DATA_FILE_PATH, sheetRowCallbackHandler, sheetCallback);
example2.process();
System.out.println("nConstructor: file, sheetRowCallbackHandler, sheetCallback");
ExcelReader example3 = new ExcelReader(file,
sheetRowCallbackHandler, null);
example3.process();
} catch (RuntimeException are) {
LOG.error(are.getMessage(), are.getCause());
} catch (InvalidFormatException ife) {
LOG.error(ife.getMessage(), ife.getCause());
} catch (IOException ioe) {
LOG.error(ioe.getMessage(), ioe.getCause());
} finally {
IOUtils.closeQuietly(inputStream);
try {
if (null != pkg) {
pkg.close();
}
} catch (IOException e) {
// just ignore IO exception
}
}
}
}
是否有一种简单的方法来读取和编辑超过50k记录的Excel文件(xls和xlsx)?我经常搜索并使用了一些可用的代码 但是我没有成功,我总是以一个或另一个例外结束。
答案 0 :(得分:4)
我得到了同样的错误:
“处理程序处理失败;嵌套异常是 java.lang.IllegalAccessError:试图访问方法 org.apache.poi.util.POILogger.log(ILjava / lang / Object;)来自类的V org.apache.poi.openxml4j.opc.PackageRelationshipCollection“
在更新maven并尝试访问eclipse中的PackageRelationshipCollection
和POILogger
类之后,对我来说工作正常。确保你有所有必需的罐子,即poi,poi-ooxml,poi-ooxml-schemas。