Java 尝试使用POI API读取*.xlsx文件

Java 尝试使用POI API读取*.xlsx文件,java,excel,apache-poi,xlsx,Java,Excel,Apache Poi,Xlsx,我试图读取一个*.xlsx文件,在控制台中收到了很多错误。我是这方面的初学者,请注意,如果我尝试阅读*.xls文件,我在那里没有问题 这是我使用的代码: public class XlsxTest { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream(new File("autotest2.xlsx"));

我试图读取一个*.xlsx文件,在控制台中收到了很多错误。我是这方面的初学者,请注意,如果我尝试阅读*.xls文件,我在那里没有问题

这是我使用的代码:

    public class XlsxTest {

    public static void main(String[] args) throws IOException {

        FileInputStream fis = new FileInputStream(new File("autotest2.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet = wb.getSheetAt(0);
        FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();

        for (Row row : sheet) {
            for (Cell cell : row) {
                switch (formulaEvaluator.evaluateInCell(cell).getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.println(cell.getNumericCellValue() + "\t\t");
                    break;

                case Cell.CELL_TYPE_STRING:
                    System.out.println(cell.getStringCellValue() + "\t\t");
                    break;
                }
            }

            System.out.println();
        }
    }
}
尝试运行此操作时,我收到以下错误:

    Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:465)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:173)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:278)
    at XlsxTest.main(XlsxTest.java:18)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
    ... 4 more
Caused by: java.lang.ExceptionInInitializerError
    at org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem.fillInType(BuiltinSchemaTypeSystem.java:1025)
    at org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem.<clinit>(BuiltinSchemaTypeSystem.java:223)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.xmlbeans.XmlBeans.getNoType(XmlBeans.java:856)
    at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:881)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:83)
    ... 9 more
Caused by: java.lang.RuntimeException: Installation Problem???  Couldn't load messages: Can't find bundle for base name org.apache.xmlbeans.impl.regex.message, locale en_US
    at org.apache.xmlbeans.impl.regex.RegexParser.setLocale(RegexParser.java:88)
    at org.apache.xmlbeans.impl.regex.RegexParser.<init>(RegexParser.java:78)
    at org.apache.xmlbeans.impl.regex.ParserForXMLSchema.<init>(ParserForXMLSchema.java:28)
    at org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:2996)
    at org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:3009)
    at org.apache.xmlbeans.impl.regex.RegularExpression.<init>(RegularExpression.java:2975)
    at org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<init>(SchemaRegularExpression.java:27)
    at org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<init>(SchemaRegularExpression.java:23)
    at org.apache.xmlbeans.impl.regex.SchemaRegularExpression$1.<init>(SchemaRegularExpression.java:44)
    at org.apache.xmlbeans.impl.regex.SchemaRegularExpression.buildKnownPatternMap(SchemaRegularExpression.java:43)
    at org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<clinit>(SchemaRegularExpression.java:38)
    ... 19 more
线程“main”org.apache.poi.POIXMLException中的异常:java.lang.reflect.InvocationTargetException 位于org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62) 在org.apache.poi.poimmldocumentpart.read(poimmldocumentpart.java:465) 位于org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:173) 位于org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:278) 在XlsxTest.main(XlsxTest.java:18) 原因:java.lang.reflect.InvocationTargetException 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源) 位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源) 位于java.lang.reflect.Constructor.newInstance(未知源) 位于org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) ... 4更多 原因:java.lang.ExceptionInInitializeError 位于org.apache.xmlbeans.impl.schema.BuiltinSchemateTypeSystem.fillInType(BuiltinSchemaTypeSystem.java:1025) 位于org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem.(BuiltinSchemaTypeSystem.java:223) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源) 位于java.lang.reflect.Method.invoke(未知源) 位于org.apache.xmlbeans.xmlbeans.getNoType(xmlbeans.java:856) 位于org.apache.xmlbeans.xmlbeans。(xmlbeans.java:881) 位于org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(未知源) 位于org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:83) ... 9更多 原因:java.lang.RuntimeException:安装问题???无法加载消息:找不到基本名称org.apache.xmlbeans.impl.regex.message、locale en_US的捆绑包 位于org.apache.xmlbeans.impl.regex.RegexParser.setLocale(RegexParser.java:88) 位于org.apache.xmlbeans.impl.regex.RegexParser.(RegexParser.java:78) 位于org.apache.xmlbeans.impl.regex.ParserForXMLSchema.(ParserForXMLSchema.java:28) 位于org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:2996) 位于org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:3009) 位于org.apache.xmlbeans.impl.regex.RegularExpression.(RegularExpression.java:2975) 位于org.apache.xmlbeans.impl.regex.SchemaRegularExpression.(SchemaRegularExpression.java:27) 位于org.apache.xmlbeans.impl.regex.SchemaRegularExpression.(SchemaRegularExpression.java:23) 位于org.apache.xmlbeans.impl.regex.SchemaRegularExpression$1(SchemaRegularExpression.java:44) 位于org.apache.xmlbeans.impl.regex.SchemaRegularExpression.buildKnownPatternMap(SchemaRegularExpression.java:43) 位于org.apache.xmlbeans.impl.regex.SchemaRegularExpression.(SchemaRegularExpression.java:38) ... 还有19个 有人能帮我解决这个问题吗?我希望我提供了必要的信息

谢谢大家!

  • 在zip中重命名lib的jar,或者简单地打开已加载lib的org/apache/xmlbeans/impl/regex
  • 查找文件message.properties(必要时查找message_xx.properties)
  • 按org/apache/xmlbeans/impl/regex路径将文件放入已用库中
  • 利润
    我在使用Apache POI时遇到了非常类似的异常:java.lang.ExceptionInInitializerError,这是由xmlbeans中的“java.lang.RuntimeException:安装问题???无法加载消息:找不到基本名称org.Apache.xmlbeans.impl.regex.message”引起的

    看起来很奇怪。代码在一台机器上工作,但在另一台机器上以这种方式失败,原因不明

    经过长时间的调试,我发现原因与Excel、POI或XMLBeans完全无关:我的项目位于以“!”结尾的文件夹中。。解决方案就是重命名文件夹


    我猜作者也发生了类似的事情,事实上,关于Libre办公室的链接帖子与这个问题无关。相反,链接的帖子应该如下:

    仅在junit测试中出现此错误。应用程序运行得非常好。所以也检查一下你的情况

    就测试而言,遵循萨尔克的建议是有效的#4有点令人困惑。下面是一个简单的过程。。适用于2.6版本的xmlbeans库

    在失败的项目中,在
    src/test/resources/org/apache/xmlbeans/impl/regex
    文件夹下创建一个名为
    message.properties
    的文件。(
    src/test/resources
    是典型的maven根资源文件夹)。在该文件中添加以下内容

    parser.parse.1=Wrong character.
    parser.parse.2=Invalid reference number.
    parser.next.1=A character is required after \\.
    parser.next.2='?' is not expected.  '(?:' or '(?=' or '(?!' or '(?<' or '(?#' or '(?>'?
    parser.next.3='(?<=' or '(?<!' is expected.
    parser.next.4=A comment is not terminated.
    parser.factor.1=')' is expected.
    parser.factor.2=Unexpected end of the pattern in a modifier group.
    parser.factor.3=':' is expected.
    parser.factor.4=Unexpected end of the pattern in a conditional group.
    parser.factor.5=A back reference or an anchor or a lookahead or a lookbehind is expected in a conditional pattern.
    parser.factor.6=There are more than three choices in a conditional group.
    parser.atom.1=A character in U+0040-U+005f must follow \\c.
    parser.atom.2=A '{' is required before a character category.
    parser.atom.3=A property name is not closed by '}'.
    parser.atom.4=Unexpected meta character.
    parser.atom.5=Unknown property.
    parser.cc.1=A POSIX character class must be closed by ':]'.
    parser.cc.2=Unexpected end of the pattern in a character class.
    parser.cc.3=Unknown name for a POSIX character class.
    parser.cc.4='-' is invalid here.
    parser.cc.5=']' is expected.
    parser.cc.6='[' is invalid in a character class.  Write '\\['.
    parser.cc.7=']' is invalid in a character class.  Write '\\]'.
    parser.cc.8='-' is an invalid character range. Write '\\-'.
    parser.ope.1='[' is expected.
    parser.ope.2=')' or '-[' or '+[' or '&[' is expected.
    parser.ope.3=The range end code point is less than the start code point.
    parser.descape.1=Invalid Unicode hex notation.
    parser.descape.2=Overflow in a hex notation.
    parser.descape.3='\\x{' must be closed by '}'.
    parser.descape.4=Invalid Unicode code point.
    parser.descape.5=An anchor must not be here.
    parser.process.1=This expression is not supported in the current option setting.
    parser.quantifier.1=Invalid quantifier. A digit is expected.
    parser.quantifier.2=Invalid quantifier. Invalid quantity or a '}' is missing.
    parser.quantifier.3=Invalid quantifier. A digit or '}' is expected.
    parser.quantifier.4=Invalid quantifier. A min quantity must be <= a max quantity.
    parser.quantifier.5=Invalid quantifier. A quantity value overflow.
    
    parser.parse.1=错误字符。
    parser.parse.2=无效的引用号。
    parser.next.1=在\\之后需要一个字符。
    parser.next.2=“?”不应为空。”(?:'或'(?='或'(?!'或'(?')?
    
    parser.next.3=(?这可能有助于感谢您的评论。不幸的是,我看到了该线程,但它对我没有帮助。我使用MS Excel创建了一个简单的xlsx文件,其中包含简单文本。共享poi jar详细信息。我获得了可用的最新版本:poi-3.13-20150929我还使用了以下jar:poi-ooxml-3.13-20150929 poi-ooxml-schemas-3.13-20150929 xmlbeans-2.6.0您还有哪些xmlbeans jar?在我看来,您也有一个旧的和/或损坏的xmlbeans拷贝