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

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

Introduction

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

Prototype

public Iterator<InputStream> getSheetsData() throws IOException, InvalidFormatException 

Source Link

Document

Returns an Iterator which will let you get at all the different Sheets in turn.

Usage

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

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException//www. j  ava 2 s  . co  m
 * @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.
 * //from   w ww  . ja v a 2  s.c  o  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//from w  w  w  .  j  a va 2s. c o  m
 * @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//www. ja va  2 s.  c o  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  ww w .  j a v  a  2  s  .co  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  a  v a  2s  . 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;
}

From source file:de.ks.idnadrev.expimp.xls.XlsxImporter.java

License:Apache License

public XlsxImportResultCollector importFromFile(File file) {
    resultCollector = new XlsxImportResultCollector();
    checkFile(file);/*  www.  j av a 2s . com*/
    OPCPackage pkg = openPackage(file);
    try {
        XSSFReader reader = new XSSFReader(pkg);
        SharedStringsTable sharedStringsTable = reader.getSharedStringsTable();//used by ms office to store all string values
        log.info("Importing from {}", file);

        Map<Integer, Collection<SingleSheetImport>> importStages = new HashMap<>();

        XSSFReader.SheetIterator iterator = (XSSFReader.SheetIterator) reader.getSheetsData();
        while (iterator.hasNext()) {
            InputStream sheetStream = iterator.next();

            String sheetName = iterator.getSheetName();
            final XlsxImportSheetResult result = resultCollector.getSheetResult(sheetName);

            Class<?> class2Import;
            try {
                class2Import = getClass().getClassLoader().loadClass(sheetName);
            } catch (ClassNotFoundException e) {
                log.info("Could not load class to import {} will skip sheet.", sheetName);
                result.generalError("Could not load class to import " + sheetName + " will skip sheet.", e);
                continue;
            }

            if (class2Import != null) {
                if (importCfg.getIgnored().contains(class2Import)) {
                    continue;
                }
                int stage = dependencyGraph.getStage(class2Import);
                importStages.putIfAbsent(stage, new LinkedList<>());
                SingleSheetImport singleSheetImport = new SingleSheetImport(class2Import, sheetStream,
                        dependencyGraph, reader, result, importCfg);
                importStages.get(stage).add(singleSheetImport);
            }
        }

        importStages.entrySet().forEach(stage -> {
            try {
                executorService.invokeAll(stage.getValue());

                List<List<Future<?>>> collect = stage.getValue().stream()//
                        .map(sheet -> sheet.getRunAfterImport().stream()
                                .map((Runnable r) -> executorService.submit(r))
                                .collect(Collectors.<Future<?>>toList()))//
                        .collect(Collectors.<List<Future<?>>>toList());

                for (List<Future<?>> futureList : collect) {
                    futureList.forEach(future -> {
                        try {
                            future.get();
                        } catch (ExecutionException e) {
                            if (throwOnError) {
                                log.error("Could not run after sheet ", e);
                                throw new RuntimeException(e);
                            }
                        } catch (InterruptedException e) {
                            //
                        }
                    });
                }
            } catch (InterruptedException e1) {
                //
            }
        });

    } catch (OpenXML4JException | IOException e) {
        resultCollector.generalError("Could not read " + file, e);
        if (throwOnError) {
            log.error("Could not read {}", file, e);
            throw new RuntimeException(e);
        }
    } finally {
        try {
            pkg.close();
        } catch (IOException e) {
            resultCollector.generalError("Could not close package " + pkg, e);
            if (throwOnError) {
                log.error("Could not close package {}", pkg, e);
            }
        }
    }
    return resultCollector;
}

From source file:de.unioninvestment.eai.portal.portlet.crud.export.streaming.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 * /*from  w  w w  .  j av  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();
        processSheet(styles, strings, stream);
        stream.close();
        ++index;
    }
}

From source file:excel.FromHowTo.java

License:Apache License

public void processFirstSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {/*from   w ww .  ja v a 2 s  .c o m*/
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        // process the first sheet
        InputStream sheet2 = r.getSheetsData().next();
        InputSource sheetSource = new InputSource(sheet2);
        parser.parse(sheetSource);
        sheet2.close();
    } finally {
        pkg.close();
    }
}

From source file:excel.FromHowTo.java

License:Apache License

public void processAllSheets(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {/*w  w  w .  j  av  a2  s  . com*/
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();

        XMLReader parser = fetchSheetParser(sst);

        Iterator<InputStream> sheets = r.getSheetsData();
        while (sheets.hasNext()) {
            System.out.println("Processing new sheet:\n");
            InputStream sheet = sheets.next();
            InputSource sheetSource = new InputSource(sheet);
            parser.parse(sheetSource);
            sheet.close();
            System.out.println("");
        }
    } finally {
        pkg.close();
    }
}