Java ApachePOI流媒体API没有';无法识别Excel(xlsx)内容

Java ApachePOI流媒体API没有';无法识别Excel(xlsx)内容,java,apache-poi,sax,xlsx,Java,Apache Poi,Sax,Xlsx,我有一个类,它接收.xlsx文件。我从这个示例中选取了它,并根据我的需要对其进行了修改: 现在,应用程序可以处理一些文件,而其他文件则完全不需要。如果我更改其中一个不工作文件中的单个字段甚至字符并再次保存它们,则整个内容都会得到正确处理。有人知道原因是什么吗 对于可能有帮助的人,以下是我的代码: package com.goodgamestudios.icosphere.service.fileReader; import com.goodgamestudios.icosphe

我有一个类,它接收.xlsx文件。我从这个示例中选取了它,并根据我的需要对其进行了修改: 现在,应用程序可以处理一些文件,而其他文件则完全不需要。如果我更改其中一个不工作文件中的单个字段甚至字符并再次保存它们,则整个内容都会得到正确处理。有人知道原因是什么吗

对于可能有帮助的人,以下是我的代码:

    package com.goodgamestudios.icosphere.service.fileReader;

    import com.goodgamestudios.icosphere.datamodel.DataSet;
    import com.goodgamestudios.icosphere.datamodel.Tuple;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.ss.usermodel.BuiltinFormats;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
    import org.apache.poi.xssf.eventusermodel.XSSFReader;
    import org.apache.poi.xssf.model.SharedStringsTable;
    import org.apache.poi.xssf.model.StylesTable;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
     import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.helpers.XMLReaderFactory;


    public class ExcelFileReader implements FileReader {

    static final Logger LOG = LoggerFactory.getLogger(ExcelFileReader.class);
    private SheetHandler handler;

    @Override
    public DataSet getDataFromFile(File file) throws IOException {

        LOG.info("Start ingesting file {}");
        try {
            OPCPackage pkg = OPCPackage.open(file);
            XSSFReader reader = new XSSFReader(pkg);
            StylesTable styles = reader.getStylesTable();
            ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);

            SharedStringsTable sst = reader.getSharedStringsTable();
            XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
            handler = new SheetHandler(styles, strings, 24);
            parser.setContentHandler(handler);

            // rId2 found by processing the Workbook
            // Seems to either be rId# or rSheet#
            System.out.println("yooooo 1");
            InputStream sheet2 = reader.getSheet("rId2");
            System.out.println("yooooo 2");
            InputSource sheetSource = new InputSource(sheet2);
            System.out.println("yooooo 3");
            parser.parse(sheetSource);
            LOG.debug("{} rows parsed", handler.getData().getRows().size() + 1);
            sheet2.close();
            return handler.getData();

        } catch (OpenXML4JException | SAXException ex) {
            LOG.warn("Unable to parse file {}", file.getName());
            LOG.warn("Exception: {}: ", ex);
        }

        return null;
    }

    /**
     * See org.xml.sax.helpers.DefaultHandler javadocs
     *
     * Derived from http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api
     * <p/>
     * Also see Standard ECMA-376, 1st edition, part 4, pages 1928ff, at
     * http://www.ecma-international.org/publications/standards/Ecma-376.htm
     * <p/>
     * A web-friendly version is http://openiso.org/Ecma/376/Part4
     */
    private static class SheetHandler extends DefaultHandler {

        boolean isFirstRow = true;
        private int quantityOfColumns;
        private int currentColumnNumber = 1;
        int currentRowNumber = 1;
        private int rowNumberOfLastCell = 1;
        private DataSet data = new DataSet();
        private Tuple tuple;

        /**
         * Table with styles
         */
        private StylesTable stylesTable;

        /**
         * Table with unique strings
         */
        private ReadOnlySharedStringsTable sharedStringsTable;

        /**
         * Number of columns to read starting with leftmost
         */
        private final int minColumnCount;

        // Set when V start element is seen
        private boolean vIsOpen;

        // Set when cell start element is seen;
        // used when cell close element is seen.
        private xssfDataType nextDataType;

        // Used to format numeric cell values.
        private short formatIndex;
        private String formatString;
        private final DataFormatter formatter;

        // The last column printed to the output stream
        private int lastColumnNumber = -1;

        // Gathers characters as they are seen.
        private StringBuffer value;

        static final Logger LOG = LoggerFactory.getLogger(SheetHandler.class);

        private SheetHandler(StylesTable styles,
                ReadOnlySharedStringsTable strings,
                int cols) {
            this.stylesTable = styles;
            this.sharedStringsTable = strings;
            this.minColumnCount = cols;
            this.value = new StringBuffer();
            this.nextDataType = xssfDataType.NUMBER;
            this.formatter = new DataFormatter();
            LOG.debug("Sheethandler created");
        }

        /*
         * (non-Javadoc)
         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
         */
        public void startElement(String uri, String localName, String name,
                Attributes attributes) throws SAXException {
            System.out.println("yooooooooooo start:uri:" + uri + " localname: " + localName + " name: " + name);
            if ("inlineStr".equals(name) || "v".equals(name)) {
                vIsOpen = true;
                // Clear contents cache
                value.setLength(0);
            } // c => cell
            else if ("c".equals(name)) {
                // Get the cell reference
                String r = attributes.getValue("r");
                int firstDigit = -1;
                for (int c = 0; c < r.length(); ++c) {
                    if (Character.isDigit(r.charAt(c))) {
                        firstDigit = c;
                        break;
                    }
                }
                currentColumnNumber = nameToColumn(r.substring(0, firstDigit));
                System.out.println("colu mn " + currentColumnNumber);

                // Set up defaults.
                this.nextDataType = xssfDataType.NUMBER;
                this.formatIndex = -1;
                this.formatString = null;
                String cellType = attributes.getValue("t");
                String cellStyleStr = attributes.getValue("s");
                if ("b".equals(cellType)) {
                    nextDataType = xssfDataType.BOOL;
                } else if ("e".equals(cellType)) {
                    nextDataType = xssfDataType.ERROR;
                } else if ("inlineStr".equals(cellType)) {
                    nextDataType = xssfDataType.INLINESTR;
                } else if ("s".equals(cellType)) {
                    nextDataType = xssfDataType.SSTINDEX;
                } else if ("str".equals(cellType)) {
                    nextDataType = xssfDataType.FORMULA;
                } else if (cellStyleStr != null) {
                    // It's a number, but almost certainly one
                    //  with a special style or format 
                    XSSFCellStyle style = null;
                    if (cellStyleStr != null) {
                        int styleIndex = Integer.parseInt(cellStyleStr);
                        style = stylesTable.getStyleAt(styleIndex);
                    } else if (stylesTable.getNumCellStyles() > 0) {
                        style = stylesTable.getStyleAt(0);
                    }
                    if (style != null) {
                        this.formatIndex = style.getDataFormat();
                        this.formatString = style.getDataFormatString();
                        if (this.formatString == null) {
                            this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex);
                        }
                    }
                }
            }

        }

        /*
         * (non-Javadoc)
         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
         */
        public void endElement(String uri, String localName, String name)
                throws SAXException {

            String thisStr = null;

            // v => contents of a cell
            if ("v".equals(name)) {
                // Process the value contents as required.
                // Do now, as characters() may be called more than once
                switch (nextDataType) {

                    case BOOL:
                        char first = value.charAt(0);
                        thisStr = first == '0' ? "FALSE" : "TRUE";
                        break;

                    case ERROR:
                        thisStr = "\"ERROR:" + value.toString() + '"';
                        break;

                    case FORMULA:
                        // A formula could result in a string value,
                        // so always add double-quote characters.
                        thisStr = '"' + value.toString() + '"';
                        break;

                    case INLINESTR:
                        // TODO: have seen an example of this, so it's untested.
                        XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
                        thisStr = '"' + rtsi.toString() + '"';
                        break;

                    case SSTINDEX:
                        String sstIndex = value.toString();
                        try {
                            int idx = Integer.parseInt(sstIndex);
                            XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                            thisStr = rtss.toString();
                        } catch (NumberFormatException ex) {
                            System.out.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString());
                        }
                        break;

                    case NUMBER:
                        String n = value.toString();
                        if (this.formatString != null && n.length() > 0) {
                            thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
                        } else {
                            thisStr = n;
                        }
                        break;

                    default:
                        thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
                        break;
                }

                // Output after we've seen the string contents
                // Emit commas for any fields that were missing on this row
                if (lastColumnNumber == -1) {
                    lastColumnNumber = 0;
                }
                for (int i = lastColumnNumber; i < currentColumnNumber; ++i) {
                }

                // Might be the empty string.
                System.out.println(thisStr);
                if (isFirstRow) {
                    data.getHeaders().add(thisStr);
                } else {
                    tuple.getRowEntries()[currentColumnNumber] = thisStr;
                }
                // Update column
                if (currentColumnNumber > -1) {
                    lastColumnNumber = currentColumnNumber;
                }

            } else if ("row".equals(name)) {

                // We're onto a new row
                System.out.println("nextrow");
                lastColumnNumber = -1;
                System.out.println("yoooooo tuple:" + tuple);
                if (isFirstRow) {
                    isFirstRow = false;
                    quantityOfColumns = data.getHeaders().size();
                    tuple = new Tuple(quantityOfColumns);

                } else if (!tuple.isEmpty()) {
                    data.addRow(tuple);
                    tuple = new Tuple(quantityOfColumns);
                }
            }

        }

        /**
         * Captures characters only if a suitable element is open. Originally
         * was just "v"; extended for inlineStr also.
         */
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if (vIsOpen) {
                value.append(ch, start, length);
            }
        }

        /**
         * Converts an Excel column name like "C" to a zero-based index.
         *
         * @param name
         * @return Index corresponding to the specified name
         */
        private int nameToColumn(String name) {
            int column = -1;
            for (int i = 0; i < name.length(); ++i) {
                int c = name.charAt(i);
                column = (column + 1) * 26 + c - 'A';
            }
            return column;
        }

        public DataSet getData() {
            return data;
        }
    }

    /**
     * The type of the data value is indicated by an attribute on the cell. The
     * value is usually in a "v" element within the cell.
     */
    enum xssfDataType {

        BOOL,
        ERROR,
        FORMULA,
        INLINESTR,
        SSTINDEX,
        NUMBER,
    }
}
package com.goodgamestudios.icosphere.service.fileReader;
导入com.goodgamestudios.icosphere.datamodel.DataSet;
导入com.goodgamestudios.icosphere.datamodel.Tuple;
导入java.io.File;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.poi.openxml4j.exceptions.openxml4jeexception;
导入org.apache.poi.openxml4j.opc.OPCPackage;
导入org.apache.poi.ss.usermodel.BuiltInformation;
导入org.apache.poi.ss.usermodel.DataFormatter;
导入org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
导入org.apache.poi.xssf.eventusermodel.XSSFReader;
导入org.apache.poi.xssf.model.SharedStringsTable;
导入org.apache.poi.xssf.model.StylesTable;
导入org.apache.poi.xssf.usermodel.XSSFCellStyle;
导入org.apache.poi.xssf.usermodel.XSSFRichTextString;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.xml.sax.Attributes;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入org.xml.sax.XMLReader;
导入org.xml.sax.helpers.DefaultHandler;
导入org.xml.sax.helpers.XMLReaderFactory;
公共类ExcelFileReader实现FileReader{
静态最终记录器日志=LoggerFactory.getLogger(ExcelFileReader.class);
私人床单处理程序;
@凌驾
公共数据集getDataFromFile(文件文件)引发IOException{
info(“开始摄取文件{}”);
试一试{
OPCPackage pkg=OPCPackage.open(文件);
XSSFReader=新XSSFReader(pkg);
StylesTable styles=reader.getStylesTable();
ReadOnlySharedStringsTable字符串=新的ReadOnlySharedStringsTable(pkg);
SharedStringsTable sst=reader.getSharedStringsTable();
XMLReader parser=XMLReaderFactory.createXMLReader(“org.apache.xerces.parsers.SAXParser”);
handler=新的SheetHandler(样式、字符串、24);
setContentHandler(handler);
//通过处理工作簿找到rId2
//似乎要么被除掉,要么被解雇#
System.out.println(“yooooo 1”);
InputStream sheet2=reader.getSheet(“rId2”);
System.out.println(“yooooo 2”);
InputSource sheetSource=新的InputSource(sheet2);
System.out.println(“yooooo 3”);
parser.parse(sheetSource);
LOG.debug(“{}行已解析”,handler.getData().getRows().size()+1);
表2.关闭();
返回处理程序.getData();
}catch(OpenXML4JException | SAXException){
warn(“无法解析文件{}”,file.getName());
warn(“异常:{}:,ex);
}
返回null;
}
/**
*请参阅org.xml.sax.helpers.DefaultHandler javadocs
*
*源自http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api
*

*另见标准ECMA-376,第1版,第4部分,第1928ff页,第 * http://www.ecma-international.org/publications/standards/Ecma-376.htm *

*一个网络友好的版本是http://openiso.org/Ecma/376/Part4 */ 私有静态类SheetHandler扩展了DefaultHandler{ 布尔值isFirstRow=true; 列的私有int数量; private int currentColumnNumber=1; int currentRowNumber=1; private int rowNumberOfLastCell=1; 私有数据集数据=新数据集(); 私有元组; /** *样式表 */ 私人风格稳定风格稳定; /** *具有唯一字符串的表 */ 私有只读共享StringStable sharedStringsTable; /** *从最左边开始读取的列数 */ 私人最终整数计数; //当看到V开始元素时设置 私人开放; //当看到单元格起始元素时设置; //当看到单元闭合元件时使用。 私有xssfDataType nextDataType; //用于格式化数值单元格值。 私人短信息索引; 私有字符串格式化字符串; 专用最终数据格式化程序格式化程序; //打印到输出流的最后一列 private int lastColumnNumber=-1; //收集看到的角色。 私有字符串缓冲区值; 静态最终记录器日志=LoggerFactory.getLogger(SheetHandler.class); 专用SheetHandler(样式稳定样式, 只读共享字符串稳定字符串, int cols){ this.stylesTable=样式; this.sharedStringsTable=字符串; this.minColumnCount=cols; this.value=新的StringBuffer(); this.nextDataType=xssfDataType.NUMBER; this.formatter=新数据格式化程序(); 调试(“创建了Sheethandler”); } /* *(非Javadoc) *@see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String、java.lang.String、java.lang.String、org.xml.sax.Attributes) */ public void startElement(字符串uri、字符串localName、字符串name、, 属性)引发SAX异常{ System.out.println(“yooooo开始:uri:+uri+”localname:+localname+”name:+name”); if(“inlineStr”.equals(name)| |“v”.equals(name)){ 可见光

private void parseFirstWorksheetWithContent(XSSFReader reader) throws IOException, InvalidFormatException, SAXException {
    //Sheet-ID seems to differ, seems to be "rId2" for files saved by MS Excel and "rId1" for those saved by LibreOffice Calc
    try {
        for (int i = 1; handler.getData().isEmpty(); i++) {
            parseSheet(reader, "rId" + i);
        }
    } catch (IllegalArgumentException e) {
        //No more sheets, file empty
    }
}

private void parseSheet(XSSFReader reader, String sheetId) throws InvalidFormatException, SAXException, IOException {
        XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
        parser.setContentHandler(handler);
        InputStream sheetStream = reader.getSheet(sheetId);
        InputSource sheetSource = new InputSource(sheetStream);
        parser.parse(sheetSource);
        sheetStream.close();
    }