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:org.talend.dataprep.schema.xls.streaming.StreamingWorkbookReader.java

License:Open Source License

void loadSheets(XSSFReader reader, SharedStringsTable sst, StylesTable stylesTable, int rowCacheSize)
        throws IOException, InvalidFormatException, XMLStreamException {
    lookupSheetNames(reader.getWorkbookData());
    Iterator<InputStream> iter = reader.getSheetsData();
    int i = 0;/*from   w w  w  .j a v  a2s.  c om*/
    while (iter.hasNext()) {
        XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next());
        sheets.add(new StreamingSheet(sheetNames.get(i++),
                new StreamingSheetReader(sst, stylesTable, parser, rowCacheSize)));
    }
}

From source file:org.talend.repository.ui.wizards.metadata.connection.files.excel.ExcelReader.java

License:Open Source License

private void init() throws BiffException, IOException {
    // hywang modified for excel 2007
    //        if (excelPath.endsWith(".xls")) { //$NON-NLS-1$
    // isXlsx = false;
    //        } else if (excelPath.endsWith(".xlsx")) { //$NON-NLS-1$
    // isXlsx = true;
    // }/*w w w .ja  v a  2s . c o m*/

    if (!isXlsx) {
        WorkbookSettings worksetting = new WorkbookSettings();
        //worksetting.setEncoding("ISO-8859-15"); //$NON-NLS-1$
        worksetting.setCellValidationDisabled(true);
        worksetting.setSuppressWarnings(true);
        workbook = Workbook.getWorkbook(new File(excelPath), worksetting);
    } else {
        // modify for bug 12174.
        File file = new File(excelPath);
        OPCPackage clone = null;
        try {
            FileInputStream in = new FileInputStream(file);
            OPCPackage open = OPCPackage.open(in);
            clone = PackageHelper.clone(open, createTempFile());
            open.close();

            // Package createPackage = Package.openOrCreate(file);
            // clone = PackageHelper.clone(createPackage);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (OpenXML4JException e) {
            e.printStackTrace();
        }
        if (clone != null) {
            List<String> sheetlist = new ArrayList<String>();
            // modified for bug TDI-26614, Use XSSF and SAX (Event API) to parse excel 2007, only need small memory
            // footprint
            if (isXlsx && (EVENT_MODE).equals(generationMode)) {
                try {
                    XSSFReader xssfReader = new XSSFReader(clone);
                    XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
                    while (sheets.hasNext()) {
                        sheets.next();
                        String sheetName = sheets.getSheetName();
                        sheetlist.add(sheetName);
                    }
                } catch (OpenXML4JException e) {
                    CommonExceptionHandler.process(e);
                }
            } else {
                xwb = new XSSFWorkbook(clone);
                for (XSSFSheet sheet : xwb) {
                    sheetlist.add(sheet.getSheetName());
                }
            }
            sheetNamesForXlsx = new String[sheetlist.size()];
            for (int i = 0; i < sheetlist.size(); i++) {
                sheetNamesForXlsx[i] = sheetlist.get(i);
            }
            sheetlist.clear();
        }
    }

}

From source file:packtest.FromHowTo.java

License:Apache License

public void processFirstSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {// w  w  w  .j  a  v a 2s.co 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:sailpoint.services.task.genericImport.ExcelSaxImport.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException//ww  w.java2  s. c o m
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */

public void process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {

    OPCPackage xlsxPackage = OPCPackage.open(xlFile.getPath(), PackageAccess.READ);

    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
        InputStream stream = iter.next();
        if (sheetName.equals(iter.getSheetName()) || sheetName == null) {
            if (log.isDebugEnabled())
                log.debug("Sheet name: " + sheetName + " (" + index + ")");
            processSheet(styles, strings, stream);
        }
        stream.close();
        ++index;
    }
    xlsxPackage.close();

}

From source file:service.ReadExcelFile.java

/**
 * Initiates the processing of the XLS workbook file to CSV.
 * /*from  w  ww  .jav a 2  s  . c om*/
 * @throws IOException
 * @throws OpenXML4JException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public void process(int sheetNo)
        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();
        if (index == sheetNo) {
            // String sheetName = iter.getSheetName();
            processSheet(styles, strings, stream);
        }
        stream.close();
        ++index;
    }
}

From source file:xlsconv.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException//from ww w . j a  va 2  s  .com
 * @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();
        // Output to separate files as well...
        if (writer != null) {
            writer.close();
        }
        writer = new PrintStream(inputFile + "_" + sheetName + "_sheet-" + index + ".csv");

        processSheet(styles, strings, stream);
        stream.close();

        writer.close();
        ++index;
    }
}