从文件读取而不是从流读取时,Apache POI引发编码错误

时间:2018-09-11 20:27:24

标签: java character-encoding apache-poi

当通过Java(7)中的Apache POI(3.17)加载特定的Excel(XLSX)文件时,出现了关于编码(org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 2 of 4-byte UTF-8 sequence)的异常。这似乎是在读取sharedStrings.xml文件时出现的(该文件使用UTF8编码)。

但是,如果我通过InputStream而不是File加载文件,则文件将正确加载。我(或我)都不能指定编码。我知道从InputStream加载不是最佳选择,我很想避免这种情况。

尽管不幸的是我无法共享有问题的文件,但我还是写了一个小例子来突出我的问题:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.MessageFormat;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class POIEncodingIssue
{
    public static void main(final String[] args) throws Exception
    {
        final File file = new File("path\\to\\my\\file.xlsx"); //$NON-NLS-1$
        Workbook workbook = null;

        // This works
        System.out.println("Trying Stream based approach..."); //$NON-NLS-1$
        try (InputStream stream = new FileInputStream(file))
        {
            workbook = WorkbookFactory.create(stream);

            System.out.println(MessageFormat.format("Value was \"{0}\"", workbook.getSheetAt(0).getRow(0).getCell(0))); //$NON-NLS-1$
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (workbook != null)
            {
                workbook.close();
            }
        }

        // This doesn't
        System.out.println("Trying File based approach..."); //$NON-NLS-1$
        try
        {
            workbook = WorkbookFactory.create(file);

            System.out.println(MessageFormat.format("Value was \"{0}\"", workbook.getSheetAt(0).getRow(0).getCell(0))); //$NON-NLS-1$
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (workbook != null)
            {
                workbook.close();
            }
        }
    }
}

以及产生的异常:

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:63)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:580)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:165)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:270)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:266)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:226)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:205)
    at com.in2.excelreader_art.DAT3983Example.main(DAT3983Example.java:42)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:60)
    ... 7 more
Caused by: java.io.IOException: Unable to parse xml bean
    at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:166)
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.SharedStringsTable.readFrom(SharedStringsTable.java:119)
    at org.apache.poi.xssf.model.SharedStringsTable.<init>(SharedStringsTable.java:107)
    ... 13 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 5443012; Invalid byte 2 of 4-byte UTF-8 sequence.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at org.apache.poi.util.DocumentHelper.readDocument(DocumentHelper.java:140)
    at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:163)
    ... 16 more
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 2 of 4-byte UTF-8 sequence.
    at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)
    at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
    at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
    at org.apache.xerces.impl.XMLEntityScanner.scanContent(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanContent(Unknown Source)
    ... 26 more

1 个答案:

答案 0 :(得分:1)

对答案发表评论

您似乎遇到了旧版本的Apache XML Beans中的错误。如果至少升级到Apache XML Beans 3.0.1,您应该会发现问题已经解决。

理想情况下,您还将至少升级到Apache POI 4.0.0,这需要更新的xmlbeans,但是需要Java 8+。 XML Beans向后兼容,因此您可以坚持使用POI 3.17并仅升级xmlbeans(尽管显然没有在4中修复POI!)