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:de.ks.idnadrev.expimp.xls.XlsxImporter.java

License:Apache License

public XlsxImportResultCollector importFromFile(File file) {
    resultCollector = new XlsxImportResultCollector();
    checkFile(file);/*www.j a va  2s  .c o m*/
    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:ec.util.spreadsheet.poi.FastPoiBook.java

License:EUPL

@Nonnull
private static FastPoiBook create(@Nonnull OPCPackage pkg) throws IOException, OpenXML4JException {
    XSSFReader reader = new XSSFReader(pkg);
    WorkbookData workbookData = new WorkbookDataSax2EventHandler().parse(newWorkBookDataSupplier(reader));
    FastPoiContext sheetContext = new FastPoiContext(
            new SharedStringsDataSax2EventHandler().parse(newSharedStringsDataSupplier(reader)),
            new StylesDataSax2EventHandler().parse(newStylesDataSupplier(reader)), workbookData.date1904);
    return new FastPoiBook(pkg, reader, workbookData.sheets, sheetContext);
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.xlsx.XLSXFileReader.java

License:Apache License

public void processSheet(InputStream inputStream, DataTable dataTable, PrintWriter tempOut) throws Exception {
    OPCPackage pkg = OPCPackage.open(inputStream);
    XSSFReader r = new XSSFReader(pkg);
    SharedStringsTable sst = r.getSharedStringsTable();

    XMLReader parser = fetchSheetParser(sst, dataTable, tempOut);

    // rId2 found by processing the Workbook
    // Seems to either be rId# or rSheet#
    InputStream sheet1 = r.getSheet("rId1");
    InputSource sheetSource = new InputSource(sheet1);
    parser.parse(sheetSource);/*from   w  ww  .  j  ava 2  s. co m*/
    sheet1.close();
}

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  www .j  a  va  2s.  com
        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 {/*from  www .j  a  v  a2  s.  c  o m*/
        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();
    }
}

From source file:excel.XSSF.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException  If reading the data from the package fails.
 * @throws SAXException if parsing the XML data fails.
 *///from   w w w.  j  av a2 s .  co  m
public void process() throws IOException, OpenXML4JException, 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 + "]:");
        this.output.println(String.format("\"%s\":[", sheetName));
        processSheet(styles, strings, new SheetToCSV(), stream);
        stream.close();
        this.output.println("]");
        ++index;
    }
}

From source file:extract.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 *
 * @throws IOException/*from   w  w  w.j  ava2 s  .  co m*/
 * @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, new SheetToCSV(), stream);
        stream.close();
        ++index;
    }
}

From source file:kiklos.tv.timetable.XLSX2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS workbook file to CSV.
 * //from w  w  w.  j ava  2s  .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;
        return; // todo: need only first sheet !!!!!
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XSSFExcelExtractorDecorator.java

License:Apache License

/**
 * @see org.apache.poi.xssf.extractor.XSSFExcelExtractor#getText()
 *//*from  ww w  . jav  a 2s. c  o m*/
@Override
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
    OPCPackage container = extractor.getPackage();

    ReadOnlySharedStringsTable strings;
    XSSFReader.SheetIterator iter;
    XSSFReader xssfReader;
    StylesTable styles;
    try {
        xssfReader = new XSSFReader(container);
        styles = xssfReader.getStylesTable();
        iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        strings = new ReadOnlySharedStringsTable(container);
    } catch (InvalidFormatException e) {
        throw new XmlException(e);
    } catch (OpenXML4JException oe) {
        throw new XmlException(oe);
    }

    while (iter.hasNext()) {
        InputStream stream = iter.next();
        sheetParts.add(iter.getSheetPart());

        SheetTextAsHTML sheetExtractor = new SheetTextAsHTML(xhtml, iter.getSheetComments());

        // Start, and output the sheet name
        xhtml.startElement("div");
        xhtml.element("h1", iter.getSheetName());

        // Extract the main sheet contents
        xhtml.startElement("table");
        xhtml.startElement("tbody");

        processSheet(sheetExtractor, styles, strings, stream);

        xhtml.endElement("tbody");
        xhtml.endElement("table");

        // Output any headers and footers
        // (Need to process the sheet to get them, so we can't
        // do the headers before the contents)
        for (String header : sheetExtractor.headers) {
            extractHeaderFooter(header, xhtml);
        }
        for (String footer : sheetExtractor.footers) {
            extractHeaderFooter(footer, xhtml);
        }
        processShapes(iter.getShapes(), xhtml);
        // All done with this sheet
        xhtml.endElement("div");
    }

    if (Config.inst().getProp(ConfigBool.ENABLE_IMAGE_OCR)) {
        TikaImageHelper helper = new TikaImageHelper(metadata);
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(container);
            List<XSSFPictureData> pictures = workbook.getAllPictures();
            for (XSSFPictureData picture : pictures) {
                ByteArrayInputStream imageData = new ByteArrayInputStream(picture.getData());
                BufferedImage image = ImageIO.read(imageData);
                helper.addImage(image);
            }
            helper.addTextToHandler(xhtml);
        } catch (Exception e) {
            // TODO:
            e.printStackTrace();
        } finally {
            if (extractor != null) {
                extractor.close();
            }
            if (helper != null) {
                helper.close();
            }
        }
    }
}