简体   繁体   English

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

[英]Trying to read *.xlsx file using POI API

I am trying to read an *.xlsx file and i receive quite a lot of errors in the console.我正在尝试读取 *.xlsx 文件,但在控制台中收到了很多错误。 I am a beginner at this and please note that if i try to read *.xls files i have no problems there.我是这方面的初学者,请注意,如果我尝试阅读 *.xls 文件,我在那里没有问题。

This is the code that i use:这是我使用的代码:

    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();
        }
    }
}

When i try to run this i receive the following errors:当我尝试运行它时,我收到以下错误:

    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

Can anyone help me with this issue?谁能帮我解决这个问题? I hope that i provided the necessary information.我希望我提供了必要的信息。

Thank you!谢谢!

  1. Rename jar of lib in zip or simply open org/apache/xmlbeans/impl/regex of loaded lib.重命名 zip 中的 lib jar 或简单地打开加载的 lib 的 org/apache/xmlbeans/impl/regex。
  2. Find file message.properties (and message_xx.properties if necessary).查找文件 message.properties(如有必要,还有 message_xx.properties)。
  3. Put file(s) in used library by org/apache/xmlbeans/impl/regex path.通过 org/apache/xmlbeans/impl/regex 路径将文件放入使用的库中。
  4. Profit!利润!

I experienced the very similar exception when using Apache POI: a java.lang.ExceptionInInitializerError, caused by "java.lang.RuntimeException: Installation Problem??? Couldn't load messages: Can't find bundle for base name org.apache.xmlbeans.impl.regex.message" in XMLBeans.我在使用 Apache POI 时遇到了非常相似的异常:java.lang.ExceptionInInitializerError,由“java.lang.RuntimeException:安装问题???无法加载消息:无法找到基本名称 org.apache 的包。 XMLBeans 中的“xmlbeans.impl.regex.message”。

Looked completely weird.看起来完全奇怪。 Code worked on one machine but failed this way on another for no apparent reason.代码在一台机器上运行,但在另一台机器上以这种方式失败,原因不明。

After LOTS of debugging hours, I found out the reason was COMPLETELY unrelated to Excel, POI or XMLBeans: my project was located in the folder ending with "!"经过大量调试时间后,我发现原因与 Excel、POI 或 XMLBeans 完全无关:我的项目位于以“!”结尾的文件夹中。 . . The solution was simply to rename the folder.解决方案只是重命名文件夹。

I guess the author had something similar happening, and indeed the linked post about Libre Office has nothing to do with this problem.我猜作者发生了类似的事情,实际上有关 Libre Office 的链接帖子与此问题无关。 Instead, the linked post should be the following: Unable to open resources in directories which end with an exclamation mark (!)相反,链接的帖子应如下所示: 无法打开以感叹号 (!) 结尾的目录中的资源

Got this error ONLY in junit test.仅在 junit 测试中出现此错误。 Application runs perfectly fine.应用程序运行得很好。 So check your case as well.所以也要检查你的情况。

For test, following Sarck's suggestion worked.为了测试,遵循 Sarck 的建议有效。 #4 is little confusing. #4 有点令人困惑。 So below is simple process.. works fine for 2.6 version of xmlbeans library.所以下面是简单的过程..适用于 2.6 版本的 xmlbeans 库。

In your project where this is failing, create a file named message.properties under src/test/resources/org/apache/xmlbeans/impl/regex folder.在失败的项目中,在src/test/resources/org/apache/xmlbeans/impl/regex文件夹下创建一个名为message.properties文件。 ( src/test/resources is typical maven root resource folder). src/test/resources是典型的 maven 根资源文件夹)。 Add following content in that file在该文件中添加以下内容

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.

Duplicate this file as message_en.properties at same location.在同一位置将此文件复制为message_en.properties

If you are using newer version of xmlbeans jar then get content of this file from Apache source - replace version in url如果您使用的是较新版本的 xmlbeans jar,则从Apache 源获取此文件的内容- 替换 url 中的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM