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:org.pentaho.di.trans.steps.excelinput.staxpoi.StaxPoiWorkbook.java

License:Apache License

public StaxPoiWorkbook(String filename, String encoding) throws KettleException {
    this();//ww w.j  a  v a  2  s .com
    try {
        opcpkg = OPCPackage.open(filename);
        openFile(opcpkg, encoding);
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.pentaho.di.trans.steps.excelinput.staxpoi.StaxPoiWorkbook.java

License:Apache License

public StaxPoiWorkbook(InputStream inputStream, String encoding) throws KettleException {
    this();//w  ww . j a  va2  s.  c o m
    try {
        opcpkg = OPCPackage.open(inputStream);
        openFile(opcpkg, encoding);
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.shareok.data.documentProcessor.WordHandler.java

private String[] readDocxFile(FileInputStream fs) throws IOException {

    String[] paragraphs = null;/*from  w  ww  .j  av  a 2s.  c  o  m*/
    try {
        //            XWPFDocument doc = new XWPFDocument();
        //            XWPFParagraph p1 = doc.createParagraph();
        //        p1.setAlignment(ParagraphAlignment.CENTER);
        //        p1.setBorderBottom(Borders.DOUBLE);
        //        p1.setBorderTop(Borders.DOUBLE);
        //
        //        p1.setBorderRight(Borders.DOUBLE);
        //        p1.setBorderLeft(Borders.DOUBLE);
        //        p1.setBorderBetween(Borders.SINGLE);
        //
        //        p1.setVerticalAlignment(TextAlignment.TOP);
        //
        //        XWPFRun r1 = p1.createRun();
        //        r1.setBold(true);
        //        r1.setText("The quick brown fox");
        //        r1.setBold(true);
        //        r1.setFontFamily("Courier");
        //        r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
        //        r1.setTextPosition(100);
        //
        //        XWPFParagraph p2 = doc.createParagraph();
        //        p2.setAlignment(ParagraphAlignment.RIGHT);
        //
        //        //BORDERS
        //        p2.setBorderBottom(Borders.DOUBLE);
        //        p2.setBorderTop(Borders.DOUBLE);
        //        p2.setBorderRight(Borders.DOUBLE);
        //        p2.setBorderLeft(Borders.DOUBLE);
        //        p2.setBorderBetween(Borders.SINGLE);
        //
        //        XWPFRun r2 = p2.createRun();
        //        r2.setText("jumped over the lazy dog");
        //        r2.setStrike(true);
        //        r2.setFontSize(20);
        //
        //        XWPFRun r3 = p2.createRun();
        //        r3.setText("and went away");
        //        r3.setStrike(true);
        //        r3.setFontSize(20);
        //        r3.setSubscript(VerticalAlign.SUPERSCRIPT);
        //
        //
        //        XWPFParagraph p3 = doc.createParagraph();
        //        p3.setWordWrap(true);
        //        p3.setPageBreak(true);
        //                
        //        //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
        //        p3.setAlignment(ParagraphAlignment.BOTH);
        //        p3.setSpacingLineRule(LineSpacingRule.EXACT);
        //
        //        p3.setIndentationFirstLine(600);
        //        
        //
        //        XWPFRun r4 = p3.createRun();
        //        r4.setTextPosition(20);
        //        r4.setText("To be, or not to be: that is the question: "
        //                + "Whether 'tis nobler in the mind to suffer "
        //                + "The slings and arrows of outrageous fortune, "
        //                + "Or to take arms against a sea of troubles, "
        //                + "And by opposing end them? To die: to sleep; ");
        //        r4.addBreak(BreakType.PAGE);
        //        r4.setText("No more; and by a sleep to say we end "
        //                + "The heart-ache and the thousand natural shocks "
        //                + "That flesh is heir to, 'tis a consummation "
        //                + "Devoutly to be wish'd. To die, to sleep; "
        //                + "To sleep: perchance to dream: ay, there's the rub; "
        //                + ".......");
        //        r4.setItalic(true);
        ////This would imply that this break shall be treated as a simple line break, and break the line after that word:
        //
        //        XWPFRun r5 = p3.createRun();
        //        r5.setTextPosition(-10);
        //        r5.setText("For in that sleep of death what dreams may come");
        //        r5.addCarriageReturn();
        //        r5.setText("When we have shuffled off this mortal coil,"
        //                + "Must give us pause: there's the respect"
        //                + "That makes calamity of so long life;");
        //        r5.addBreak();
        //        r5.setText("For who would bear the whips and scorns of time,"
        //                + "The oppressor's wrong, the proud man's contumely,");
        //        
        //        r5.addBreak(BreakClear.ALL);
        //        r5.setText("The pangs of despised love, the law's delay,"
        //                + "The insolence of office and the spurns" + ".......");
        //
        //        FileOutputStream out = new FileOutputStream("simple.docx");
        //        doc.write(out);
        //        out.close();
        XWPFDocument document = new XWPFDocument(OPCPackage.open("simple.docx"));
        List<XWPFParagraph> paragraphList = document.getParagraphs();
        paragraphs = new String[paragraphList.size()];
        int i = 0;
        for (XWPFParagraph para : paragraphList) {
            paragraphs[i] = para.getText();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        fs.close();
    }
    return paragraphs;
}

From source file:org.soulwing.jawb.poi.ApachePoiExtractor.java

License:Apache License

public BoundWorkbook newWorkbook(InputStream inputStream, Validator validator)
        throws WorkbookBindingException, IOException {
    try {/*from w  w  w  .j av  a  2  s  .c o m*/
        OPCPackage pkg = OPCPackage.open(inputStream);
        XSSFWorkbook workbook = new XSSFWorkbook(pkg);
        return new ApachePoiWorkbook(workbook, validator);
    } catch (InvalidFormatException ex) {
        throw new WorkbookBindingException(ex);
    }
}

From source file:org.specrunner.source.excel.SourceFactoryExcel.java

License:Open Source License

/**
 * Load a document from a target./* w ww .  jav a2  s . c  om*/
 * 
 * @param uri
 *            The target corresponding uri (if any).
 * @param target
 *            The target.
 * @param encoding
 *            The encoding.
 * @return The document, if exists, null, otherwise.
 * @throws SourceException
 *             On load error.
 */
@Override
protected Document fromTarget(URI uri, String target, String encoding) throws SourceException {
    Element html = new Element("html");
    Document result = new Document(html);
    OPCPackage pkg = null;
    InputStream in = null;
    POIFSFileSystem fsys = null;
    try {
        Workbook wb = null;
        if (isFile(uri, target)) {
            if (UtilLog.LOG.isDebugEnabled()) {
                UtilLog.LOG.debug("Source from file:" + target);
            }
            in = new FileInputStream(new File(target));
        } else {
            if (UtilLog.LOG.isDebugEnabled()) {
                UtilLog.LOG.debug("Source from URI:" + uri);
            }
            in = uri.toURL().openStream();
        }
        if (target.trim().toLowerCase().endsWith(XLSX)) {
            pkg = OPCPackage.open(in);
            wb = new XSSFWorkbook(pkg);
        } else {
            fsys = new POIFSFileSystem(in);
            wb = new HSSFWorkbook(fsys);
        }
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sheet = wb.getSheetAt(i);
            Map<String, Dimension> spanMap = new HashMap<String, Dimension>();
            Set<String> ignoreMap = new HashSet<String>();
            for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
                CellRangeAddress region = sheet.getMergedRegion(j);
                for (int x = region.getFirstRow(); x <= region.getLastRow(); x++) {
                    for (int y = region.getFirstColumn(); y <= region.getLastColumn(); y++) {
                        if (x == region.getFirstRow() && y == region.getFirstColumn()) {
                            spanMap.put(x + "," + y,
                                    new Dimension(region.getLastRow() - x + 1, region.getLastColumn() - y + 1));
                        } else {
                            ignoreMap.add(x + "," + y);
                        }
                    }
                }
            }
            Element table = new Element("table");
            table.addAttribute(new Attribute("border", "1"));
            html.appendChild(table);
            Element caption = readCaption(table, sheet);
            Iterator<Row> ite = sheet.iterator();
            readBody(table, caption, spanMap, ignoreMap, ite, headers(table, caption, spanMap, ignoreMap, ite));
        }
    } catch (Exception e) {
        if (UtilLog.LOG.isDebugEnabled()) {
            UtilLog.LOG.debug(e.getMessage(), e);
        }
        throw new SourceException(e);
    } finally {
        if (pkg != null) {
            try {
                pkg.close();
            } catch (IOException e) {
                if (UtilLog.LOG.isDebugEnabled()) {
                    UtilLog.LOG.debug(e.getMessage(), e);
                }
                throw new SourceException(e);
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                if (UtilLog.LOG.isDebugEnabled()) {
                    UtilLog.LOG.debug(e.getMessage(), e);
                }
                throw new SourceException(e);
            }
        }
    }
    return result;
}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingSheetTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    OPCPackage pkg = OPCPackage.open(StreamingSheetTest.class.getResourceAsStream("../dates.xlsx"));
    XSSFReader reader = new XSSFReader(pkg);

    SharedStringsTable sst = reader.getSharedStringsTable();
    StylesTable styles = reader.getStylesTable();

    Iterator<InputStream> iter = reader.getSheetsData();
    XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next());
    final StreamingSheetReader streamingSheetReader = new StreamingSheetReader(sst, styles, parser, 10);
    streamingSheet = new StreamingSheet("name", streamingSheetReader);
}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingWorkbookReader.java

License:Open Source License

public void init(File f) {
    try {/*  w  w w  .j a v a 2s  . c o  m*/
        if (builder.getPassword() != null) {
            // Based on: https://poi.apache.org/encryption.html
            POIFSFileSystem poifs = new POIFSFileSystem(f);
            EncryptionInfo info = new EncryptionInfo(poifs);
            Decryptor d = Decryptor.getInstance(info);
            d.verifyPassword(builder.getPassword());
            pkg = OPCPackage.open(d.getDataStream(poifs));
        } else {
            pkg = OPCPackage.open(f);
        }

        XSSFReader reader = new XSSFReader(pkg);

        SharedStringsTable sst = reader.getSharedStringsTable();
        StylesTable styles = reader.getStylesTable();

        loadSheets(reader, sst, styles, builder.getRowCacheSize());
    } catch (IOException e) {
        throw new OpenException("Failed to open file", e);
    } catch (OpenXML4JException | XMLStreamException e) {
        throw new ReadException("Unable to read workbook", e);
    } catch (GeneralSecurityException e) {
        throw new ReadException("Unable to read workbook - Decryption failed", e);
    }
}

From source file:org.talend.dataprep.schema.xls.XlsUtilsTest.java

License:Open Source License

@Test
public void get_active_sheets() throws Exception {

    OPCPackage container = OPCPackage.open(getClass().getResourceAsStream("000_DTA_DailyTimeLog.xlsm"));
    XSSFReader xssfReader = new XSSFReader(container);

    List<String> names = XlsUtils.getActiveSheetsFromWorkbookSpec(xssfReader.getWorkbookData());

    Assertions.assertThat(names).isNotEmpty() //
            .containsExactly("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY",
                    "WEEK SUMMARY");

}

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;
    // }/*from   w  w  w  .j  a v  a2  s.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:org.ujmp.poi.MatrixXLSXImporter.java

License:Open Source License

public DenseObjectMatrix2D importFromXLSX(final File file, final int sheetNumber)
        throws InvalidFormatException, IOException {
    final OPCPackage pkg = OPCPackage.open(file);
    final XSSFWorkbook workbook = new XSSFWorkbook(pkg);
    final DenseObjectMatrix2D matrix = importFromWorkbook(workbook, sheetNumber);
    pkg.close();/* w  w  w .j a  v  a2 s .c  o m*/
    return matrix;
}