Example usage for org.apache.poi.openxml4j.opc OPCPackage open

List of usage examples for org.apache.poi.openxml4j.opc OPCPackage open

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.opc OPCPackage open.

Prototype

public static OPCPackage open(InputStream in) throws InvalidFormatException, IOException 

Source Link

Document

Open a package.

Usage

From source file:com.yqboots.initializer.core.builder.excel.ProjectFileBuilder.java

License:Apache License

@Override
public List<Path> getFiles(final Path root, final ProjectMetadata metadata, InputStream inputStream)
        throws IOException {
    final List<Path> results = new ArrayList<>();
    try (XSSFWorkbook workbook = new XSSFWorkbook(OPCPackage.open(inputStream))) {
        for (final Sheet sheet : workbook) {
            // one sheet can be processed by more than one builder
            for (SheetBuilder builder : sheetBuilders) {
                if (builder.supports(sheet)) {
                    builder.build(root, metadata, sheet);
                }/*ww  w .  j a va  2 s.  c  om*/
            }
        }

        return results;
    } catch (InvalidFormatException e) {
        LOG.error(e.getMessage(), e);
        throw new IOException(e);
    }
}

From source file:containerMath.containerMath.java

public void readFromExcel(String pathToFile, String name, String range, String numberOfItems, String inPack,
        String numberOfPacks, String netWeight, String sumNetWeight, String grossWeight, String sumGrossWeight,
        String volumeOfPack, String sumVolume) {
    //{/*from  ww  w. j  av  a  2s. c  o  m*/
    //        cont.rowsRangeProcessing(range);
    int itemName = CellReference.convertColStringToIndex(name);
    int itemsQuantity = CellReference.convertColStringToIndex(numberOfItems);
    int inPackIndex = CellReference.convertColStringToIndex(inPack);
    int numOfPacksIndex = CellReference.convertColStringToIndex(numberOfPacks);
    int netWeightIndex = CellReference.convertColStringToIndex(netWeight);
    int sumNetWeightIndex = CellReference.convertColStringToIndex(sumNetWeight);
    int grossWeightIndex = CellReference.convertColStringToIndex(grossWeight);
    int sumGrossWeightIndex = CellReference.convertColStringToIndex(sumGrossWeight);
    int volumeOfPackIndex = CellReference.convertColStringToIndex(volumeOfPack);
    int sumVolumeIndex = CellReference.convertColStringToIndex(sumVolume);
    try {
        OPCPackage pkg;
        try {
            pkg = OPCPackage.open(new File(pathToFile));
            // pkg = OPCPackage.open(new File("/home/igor/Documents/China/HDHardware/test.xlsx"));

            XSSFWorkbook book = new XSSFWorkbook(pkg);
            Sheet sheet1 = book.getSheetAt(0);
            //Sheet sheet2 = book.createSheet();
            XSSFWorkbook book2 = new XSSFWorkbook();
            Sheet bookSheet = book2.createSheet();

            items = new ArrayList<Item>();
            //itemsList = new Instances();
            for (int n = cont.getFirstNumber(); n < cont.getSecondNumber(); n++) {
                Row row = sheet1.getRow(n);
                Item item = new Item(row.getCell(itemName).toString(),
                        row.getCell(itemsQuantity).getNumericCellValue(),
                        row.getCell(inPackIndex).getNumericCellValue(),
                        row.getCell(numOfPacksIndex).getNumericCellValue(),
                        (int) row.getCell(grossWeightIndex).getNumericCellValue(),
                        row.getCell(sumGrossWeightIndex).getNumericCellValue(),
                        row.getCell(volumeOfPackIndex).getNumericCellValue(),
                        row.getCell(sumVolumeIndex).getNumericCellValue());
                items.add(item);
            }
            this.numberOfItems = items.size();
            allWeight();
            allVolume();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } catch (InvalidFormatException ex) {
        ex.printStackTrace();
    }
}

From source file:Core.Core.java

/**
 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 *  the given InputStream./*  w  w w . java  2  s .  co  m*/
 * Your input stream MUST either support mark/reset, or
 *  be wrapped as a {@link PushbackInputStream}!
 */
public Workbook create(InputStream inp) throws InvalidFormatException, IOException {
    // If clearly doesn't do mark/reset, wrap up
    if (!inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }

    if (POIFSFileSystem.hasPOIFSHeader(inp)) {
        return new HSSFWorkbook(inp);
    }
    if (POIXMLDocument.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");

}

From source file:de.iisys.schub.processMining.similarity.parsing.DocxParser.java

License:Apache License

private XWPFDocument readFile(InputStream is) {
    if (is != null) {
        try {//w  ww  . ja  va  2  s  .c o m
            XWPFDocument doc = new XWPFDocument(OPCPackage.open(is));
            is.close();
            return doc;
        } catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

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

License:Apache License

protected OPCPackage openPackage(File file) {
    OPCPackage pkg = null;//from   w w w  .ja v a  2  s  .com
    try {
        pkg = OPCPackage.open(file);
    } catch (InvalidFormatException e) {
        log.error("Could not create opc package for file {}", file, e);
        throw new RuntimeException(e);
    }
    return pkg;
}

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

License:Apache License

private InputStream createCSVFromExcel(final InputStream fis) {
    File tempCsvFile = null;/*  w  w  w  .j ava 2  s  . co m*/
    PrintStream csvFileOut = null;
    try {
        tempCsvFile = File.createTempFile("tmp", ".csv");
        csvFileOut = new PrintStream(new BufferedOutputStream(new FileOutputStream(tempCsvFile, true)));

        OPCPackage p = OPCPackage.open(fis);
        XLSX2CSV xlsx2csv = new XLSX2CSV(p, csvFileOut, -1);
        xlsx2csv.process();

        return new DeletingFileInputStream(tempCsvFile);

    } catch (Exception e) {
        LOGGER.error("Error creating CSV", e);
        return null;

    } finally {
        if (csvFileOut != null) {
            try {
                csvFileOut.close();
            } catch (final RuntimeException e) {
            }
        }
        tempCsvFile.deleteOnExit();
    }
}

From source file:dk.defxws.fedoragsearch.server.TransformerToText.java

License:Open Source License

private static Stream getTextFromDOCX(InputStream doc) throws GenericSearchException {
    long time = System.currentTimeMillis();
    boolean errorFlag = Boolean.parseBoolean(Config.getCurrentConfig().getIgnoreTextExtractionErrors());
    XWPFWordExtractor wordExtractor = null;
    try {/*from   w w  w . ja v  a 2  s .  c  o m*/
        wordExtractor = new XWPFWordExtractor(OPCPackage.open(doc));
        StringBuffer buffer = new StringBuffer(wordExtractor.getText().trim());
        Stream stream = new Stream();
        stream.write(buffer.toString().getBytes(Constants.XML_CHARACTER_ENCODING));
        stream.lock();
        if (logger.isDebugEnabled()) {
            logger.debug("extracting text from docx needed " + (System.currentTimeMillis() - time));
        }
        return stream;
    } catch (Exception e) {
        if (errorFlag) {
            logger.warn("", e);
            return createErrorStream(docxTextExtractionErrorString);
        } else {
            throw new GenericSearchException("cannot parse docx-file", e);
        }
    } finally {
        wordExtractor = null;
    }
}

From source file:dk.defxws.fedoragsearch.server.TransformerToText.java

License:Open Source License

private static Stream getTextFromPPTX(InputStream doc) throws GenericSearchException {
    long time = System.currentTimeMillis();
    boolean errorFlag = Boolean.parseBoolean(Config.getCurrentConfig().getIgnoreTextExtractionErrors());
    XSLFPowerPointExtractor powerPointExtractor = null;
    try {/*from  www  .  j  a  v  a  2s .  c  o m*/
        powerPointExtractor = new XSLFPowerPointExtractor(OPCPackage.open(doc));
        StringBuffer buffer = new StringBuffer(powerPointExtractor.getText(true, true).trim());
        Stream stream = new Stream();
        stream.write(buffer.toString().getBytes(Constants.XML_CHARACTER_ENCODING));
        stream.lock();
        if (logger.isDebugEnabled()) {
            logger.debug("extracting text from pptx needed " + (System.currentTimeMillis() - time));
        }
        return stream;
    } catch (Exception e) {
        if (errorFlag) {
            logger.warn("", e);
            return createErrorStream(pptxTextExtractionErrorString);
        } else {
            throw new GenericSearchException("cannot parse pptx-file", e);
        }
    } finally {
        powerPointExtractor = null;
    }
}

From source file:dk.defxws.fedoragsearch.server.TransformerToText.java

License:Open Source License

private static Stream getTextFromXLSX(InputStream doc) throws GenericSearchException {
    long time = System.currentTimeMillis();
    boolean errorFlag = Boolean.parseBoolean(Config.getCurrentConfig().getIgnoreTextExtractionErrors());
    XSSFExcelExtractor excelExtractor = null;
    try {//from w w  w . j av  a 2 s .c  o m
        excelExtractor = new XSSFExcelExtractor(OPCPackage.open(doc));
        StringBuffer buffer = new StringBuffer(excelExtractor.getText().trim());
        Stream stream = new Stream();
        stream.write(buffer.toString().getBytes(Constants.XML_CHARACTER_ENCODING));
        stream.lock();
        if (logger.isDebugEnabled()) {
            logger.debug("extracting text from xlsx needed " + (System.currentTimeMillis() - time));
        }
        return stream;
    } catch (Exception e) {
        if (errorFlag) {
            logger.warn("", e);
            return createErrorStream(xlsxTextExtractionErrorString);
        } else {
            throw new GenericSearchException("cannot parse xlsx-file", e);
        }
    } finally {
        excelExtractor = null;
    }
}

From source file:ec.util.spreadsheet.poi.FastPoiBook.java

License:EUPL

/**
 * Opens a book from a stream./*from w ww. j  a v  a  2s  . c  o  m*/
 *
 * Note from OPCPackage: uses quite a bit more memory than
 * {@link #create(File)}, which doesn't need to hold the whole zip file in
 * memory, and can take advantage of native methods
 *
 * @param stream
 * @return
 * @throws IOException
 * @throws OpenXML4JException
 */
@Nonnull
public static FastPoiBook create(@Nonnull InputStream stream) throws IOException, OpenXML4JException {
    return create(OPCPackage.open(stream));
}