Example usage for org.apache.poi.xssf.eventusermodel XSSFReader XSSFReader

List of usage examples for org.apache.poi.xssf.eventusermodel XSSFReader XSSFReader

Introduction

In this page you can find the example usage for org.apache.poi.xssf.eventusermodel XSSFReader XSSFReader.

Prototype

public XSSFReader(OPCPackage pkg) throws IOException, OpenXML4JException 

Source Link

Document

Creates a new XSSFReader, for the given package

Usage

From source file:com.sonicle.webtop.core.io.input.ExcelFileReader.java

License:Open Source License

public List<String> listXlsxSheets(File file) throws IOException, FileReaderException {
    ArrayList<String> sheets = new ArrayList<>();
    OPCPackage opc = null;//from www.ja va 2  s .  co  m

    try {
        opc = OPCPackage.open(file, PackageAccess.READ);
        XSSFReader reader = new XSSFReader(opc);
        XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData();
        while (sit.hasNext()) {
            InputStream is = null;
            try {
                is = sit.next();
                String name = sit.getSheetName();
                if (name != null)
                    sheets.add(name);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (OpenXML4JException ex) {
        throw new FileReaderException(ex, "Error opening file");
    } finally {
        IOUtils.closeQuietly(opc);
    }
    return sheets;
}

From source file:com.sonicle.webtop.core.io.input.ExcelFileReader.java

License:Open Source License

public HashMap<String, String> listXlsxColumnNames(File file) throws IOException, FileReaderException {
    OPCPackage opc = null;/*  w  w  w .j  a  va  2s  . c  om*/

    try {
        opc = OPCPackage.open(file, PackageAccess.READ);
        XSSFReader reader = new XSSFReader(opc);
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opc);
        StylesTable styles = reader.getStylesTable();

        XlsxColumnsHandler columnsHandler = null;
        XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData();
        while (sit.hasNext()) {
            InputStream is = null;
            try {
                is = sit.next();
                if (StringUtils.equals(sit.getSheetName(), sheet)) {
                    XMLReader xmlReader = SAXHelper.newXMLReader();
                    columnsHandler = new XlsxColumnsHandler(is, headersRow, firstDataRow, lastDataRow);
                    ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, columnsHandler, fmt,
                            false);
                    xmlReader.setContentHandler(handler);
                    xmlReader.parse(new InputSource(is));
                }
            } catch (SAXException | ParserConfigurationException ex) {
                throw new FileReaderException(ex, "Error processing file content");
            } catch (NullPointerException ex) {
                // Thrown when stream is forcibly closed. Simply ignore this!
            } finally {
                IOUtils.closeQuietly(is);
            }
            if (columnsHandler != null)
                break;
        }
        return columnsHandler.columnNames;

    } catch (OpenXML4JException | SAXException ex) {
        throw new FileReaderException(ex, "Error opening file");
    } finally {
        IOUtils.closeQuietly(opc);
    }
}

From source file:com.sonicle.webtop.core.io.input.ExcelFileReader.java

License:Open Source License

public HashMap<String, Integer> listXlsxColumnIndexes(File file) throws IOException, FileReaderException {
    OPCPackage opc = null;//from   www  .  j  av  a2  s  .  c  om

    try {
        opc = OPCPackage.open(file, PackageAccess.READ);
        XSSFReader reader = new XSSFReader(opc);
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opc);
        StylesTable styles = reader.getStylesTable();

        XlsxColumnsHandler columnsHandler = null;
        XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData();
        while (sit.hasNext()) {
            InputStream is = null;
            try {
                is = sit.next();
                if (StringUtils.equals(sit.getSheetName(), sheet)) {
                    XMLReader xmlReader = SAXHelper.newXMLReader();
                    columnsHandler = new XlsxColumnsHandler(is, headersRow, firstDataRow, lastDataRow);
                    ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, columnsHandler, fmt,
                            false);
                    xmlReader.setContentHandler(handler);
                    xmlReader.parse(new InputSource(is));
                }
            } catch (SAXException | ParserConfigurationException ex) {
                throw new FileReaderException(ex, "Error processing file content");
            } catch (NullPointerException ex) {
                // Thrown when stream is forcibly closed. Simply ignore this!
            } finally {
                IOUtils.closeQuietly(is);
            }
            if (columnsHandler != null)
                break;
        }
        return columnsHandler.columnIndexes;

    } catch (OpenXML4JException | SAXException ex) {
        throw new FileReaderException(ex, "Error opening file");
    } finally {
        IOUtils.closeQuietly(opc);
    }
}

From source file:com.talend.excel.xssf.event.ExcelReader.java

License:Open Source License

public Object call() throws Exception {
    OPCPackage pkg = null;/*w  ww . j a va  2  s.c o  m*/
    try {
        if (fileURL != null) {
            pkg = OPCPackage.open(fileURL);
        } else {
            pkg = PackageHelper.open(is);
        }
        XSSFReader r = new XSSFReader(pkg);

        StylesTable styles = r.getStylesTable();
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
        sheetContentsHandler = new DefaultTalendSheetContentsHandler(cache);
        DataFormatter formatter = new DataFormatter();
        boolean formulasNotResults = false;

        XMLReader parser = XMLReaderFactory.createXMLReader();
        ContentHandler handler = new TalendXSSFSheetXMLHandler(styles, strings, sheetContentsHandler, formatter,
                formulasNotResults);
        parser.setContentHandler(handler);

        XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) r.getSheetsData();
        List<InputStream> iss = new ArrayList<InputStream>();
        while (sheets.hasNext()) {
            InputStream sheet = sheets.next();
            String sheetName = sheets.getSheetName();

            boolean match = false;

            for (int i = 0; i < sheetNames.size(); i++) {
                if ((asRegexs.get(i) && sheetName.matches(sheetNames.get(i)))
                        || (!asRegexs.get(i) && sheetName.equals(sheetNames.get(i)))) {
                    match = true;
                    iss.add(sheet);
                    break;
                }
            }

            if (!match) {
                sheet.close();
            }
        }

        if (iss.size() < 1) {
            throw new RuntimeException("No match sheets");
        }

        for (InputStream is : iss) {
            try {
                InputSource sheetSource = new InputSource(is);
                sheetSource.setEncoding(charset);
                parser.parse(sheetSource);
            } finally {
                is.close();
            }
        }

    } finally {
        if (pkg != null) {
            pkg.revert();
        }
        cache.notifyErrorOccurred();
    }
    return null;
}

From source file:com.test.demo.ccbpay.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException//from   www  . j  a va 2s. c  om
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private List<String[]> process()
        throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    while (it.hasNext()) {
        InputStream stream = it.next();
        processSheet(styles, strings, new SheetToCSV(this.minColumns), stream);
        stream.close();
    }
    return this.rows;
}

From source file:com.test.excel.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 * /* w  w w  . j a v  a 2s  .  co m*/
 * @throws IOException
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public void process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {
    List<String[]> list = null;
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        String sheetName = iter.getSheetName();
        list = processSheet(styles, strings, stream);
        stream.close();
        ++index;
    }
    System.out.println(list);
    close();
}

From source file:com.toolsverse.etl.connector.excel.ExcelXlsxConnector.java

License:Open Source License

/**
 * Initiates the processing of the XLS workbook file.
 *
 * @param xlsxPackage the xlsx package/* w  w  w.ja  va  2 s .  c  om*/
 * @param sheetName the sheet name
 * @param params the params
 * @param dataSet the data set
 * @param driver the driver
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws OpenXML4JException the open xm l4 j exception
 * @throws ParserConfigurationException the parser configuration exception
 * @throws SAXException the sAX exception
 */
public void process(OPCPackage xlsxPackage, String sheetName, ExcelConnectorParams params, DataSet dataSet,
        Driver driver) throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        try {
            if (sheetName.equals(iter.getSheetName())) {
                processSheet(styles, strings, stream, params, dataSet, driver);

                return;
            }
        } finally {
            stream.close();
        }
    }
}

From source file:com.tsm.xlstocsv.XlsxToCsv.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException/*  ww w  .  j  a  v a  2 s . co  m*/
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 * @throws javax.xml.parsers.ParserConfigurationException
 * @throws org.xml.sax.SAXException
 */
public void process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {

    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        PrintStream output = dispatcher.openStreamForSheet(iter.getSheetName());
        processSheet(styles, strings, stream, output);
        dispatcher.closeStreamForSheet(output, iter.getSheetName());
        stream.close();
        ++index;
    }
}

From source file:com.ufo.util.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 * /*from  w ww .  j a v a  2  s. c o  m*/
 * @throws IOException
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public void process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {

    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        String sheetName = iter.getSheetName();
        //         this.output.println();
        //         this.output.println(sheetName + " [index=" + index + "]:");
        processSheet(styles, strings, stream);
        stream.close();
        ++index;
    }
}

From source file:com.xipin.est.ucontroller.excel.ExcelImportXLSXUtil.java

License:Apache License

/**
 * ??? /* w w w.  j av  a  2 s .  c o  m*/
 * 
 * @throws IOException
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public List<String[]> process()
        throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {

    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    List<String[]> list = null;
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        String sheetNameTemp = iter.getSheetName();
        if (this.sheetName.equals(sheetNameTemp)) {
            list = processSheet(styles, strings, stream);
            stream.close();
        }
    }
    return list;
}