簡體   English   中英

嘗試使用 POI API 讀取 *.xlsx 文件

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

我正在嘗試讀取 *.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

誰能幫我解決這個問題? 我希望我提供了必要的信息。

謝謝!

  1. 重命名 zip 中的 lib jar 或簡單地打開加載的 lib 的 org/apache/xmlbeans/impl/regex。
  2. 查找文件 message.properties(如有必要,還有 message_xx.properties)。
  3. 通過 org/apache/xmlbeans/impl/regex 路徑將文件放入使用的庫中。
  4. 利潤!

我在使用 Apache POI 時遇到了非常相似的異常:java.lang.ExceptionInInitializerError,由“java.lang.RuntimeException:安裝問題???無法加載消息:無法找到基本名稱 org.apache 的包。 XMLBeans 中的“xmlbeans.impl.regex.message”。

看起來完全奇怪。 代碼在一台機器上運行,但在另一台機器上以這種方式失敗,原因不明。

經過大量調試時間后,我發現原因與 Excel、POI 或 XMLBeans 完全無關:我的項目位於以“!”結尾的文件夾中。 . 解決方案只是重命名文件夾。

我猜作者發生了類似的事情,實際上有關 Libre Office 的鏈接帖子與此問題無關。 相反,鏈接的帖子應如下所示: 無法打開以感嘆號 (!) 結尾的目錄中的資源

僅在 junit 測試中出現此錯誤。 應用程序運行得很好。 所以也要檢查你的情況。

為了測試,遵循 Sarck 的建議有效。 #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.

在同一位置將此文件復制為message_en.properties

如果您使用的是較新版本的 xmlbeans jar,則從Apache 源獲取此文件的內容- 替換 url 中的版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM