Example usage for org.apache.poi.ss.usermodel WorkbookFactory create

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory create.

Prototype

public static Workbook create(File file) throws IOException, EncryptedDocumentException 

Source Link

Document

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and be readable.

Usage

From source file:com.github.crab2died.ExcelUtils.java

License:Open Source License

/**
 * ?Excel?,{@code List[List[String]]}??//from w w  w.  j a va2  s  .  c  o  m
 *
 * @param excelPath  ?Excel
 * @param offsetLine Excel(0)
 * @return {@code List<List<String>>}??
 * @throws IOException            
 * @throws InvalidFormatException 
 * @author Crab2Died
 */
public List<List<String>> readExcel2List(String excelPath, int offsetLine)
        throws IOException, InvalidFormatException {

    try (Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(excelPath)))) {
        return readExcel2ObjectsHandler(workbook, offsetLine, Integer.MAX_VALUE, 0);
    }
}

From source file:com.github.crab2died.ExcelUtils.java

License:Open Source License

/**
 * ?Excel?,{@code List[List[String]]}??/*www. j a v a2s .c  o m*/
 *
 * @param is         ?Excel??
 * @param offsetLine Excel(0)
 * @return {@code List<List<String>>}??
 * @throws IOException            
 * @throws InvalidFormatException 
 * @author Crab2Died
 */
public List<List<String>> readExcel2List(InputStream is, int offsetLine)
        throws IOException, InvalidFormatException {

    try (Workbook workbook = WorkbookFactory.create(is)) {
        return readExcel2ObjectsHandler(workbook, offsetLine, Integer.MAX_VALUE, 0);
    }
}

From source file:com.github.crab2died.ExcelUtils.java

License:Open Source License

/**
 * ?Excel?,{@code List[List[String]]}??//from  w  w  w.  j a  va2s  .  c  om
 *
 * @param excelPath ?Excel
 * @return {@code List<List<String>>}??
 * @throws IOException            
 * @throws InvalidFormatException 
 * @author Crab2Died
 */
public List<List<String>> readExcel2List(String excelPath) throws IOException, InvalidFormatException {

    try (Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(excelPath)))) {
        return readExcel2ObjectsHandler(workbook, 0, Integer.MAX_VALUE, 0);
    }
}

From source file:com.github.crab2died.ExcelUtils.java

License:Open Source License

/**
 * ?Excel?,{@code List[List[String]]}??/*from   ww w  . j a va  2 s  . com*/
 *
 * @param is ?Excel??
 * @return {@code List<List<String>>}??
 * @throws IOException            
 * @throws InvalidFormatException 
 * @author Crab2Died
 */
public List<List<String>> readExcel2List(InputStream is) throws IOException, InvalidFormatException {

    try (Workbook workbook = WorkbookFactory.create(is)) {
        return readExcel2ObjectsHandler(workbook, 0, Integer.MAX_VALUE, 0);
    }
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

public static SheetTemplate sheetTemplateBuilder(String templatePath) throws Excel4JException {
    SheetTemplate sheetTemplate = new SheetTemplate();
    try {/*from ww w  . ja  v a  2s  .  c  om*/
        // ??
        sheetTemplate.workbook = WorkbookFactory.create(new FileInputStream(new File(templatePath)));
    } catch (Exception e) {
        // ??
        try {
            sheetTemplate.workbook = WorkbookFactory
                    .create(SheetTemplateHandler.class.getResourceAsStream(templatePath));
        } catch (IOException | InvalidFormatException e1) {
            throw new Excel4JException(e1);
        }
    }
    return sheetTemplate;
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

public static SheetTemplate sheetTemplateBuilder(InputStream is) throws Excel4JException {
    SheetTemplate sheetTemplate = new SheetTemplate();

    // ??//from   w ww.j  ava2 s.  c om
    try {
        sheetTemplate.workbook = WorkbookFactory.create(is);
    } catch (IOException | InvalidFormatException e) {
        throw new Excel4JException(e);
    }
    return sheetTemplate;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiDocumentFactory.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  ww  w  .ja  va 2s . com
protected Workbook loadSpreadsheetDocument(final InputStream inputStream) throws SpreadsheetException {
    Workbook workbook;
    try {
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
    } catch (final InvalidFormatException e) {
        throw new SpreadsheetException(e);
    } catch (final IOException e) {
        throw new SpreadsheetException(e);
    }
    return workbook;
}

From source file:com.github.ko2ic.plugin.eclipse.taggen.core.domain.model.spreadsheet.Workbook.java

License:Open Source License

public Workbook(InputStream is) {
    try {/*from  w  w w.j a  v  a 2 s . c  o  m*/
        workbook = WorkbookFactory.create(is);
    } catch (InvalidFormatException | IOException e) {
        log.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage()));
    }
}

From source file:com.github.wnameless.workbookaccessor.WorkbookReader.java

License:Apache License

private Workbook createWorkbook(File file) {
    try {//from www.j a  va2  s .co  m
        fis = new FileInputStream(file);
        return WorkbookFactory.create(fis);
    } catch (Exception e) {
        log.error(null, e);
        throw new RuntimeException(e);
    }
}

From source file:com.glodon.tika.UpdateEmbeddedDoc.java

License:Apache License

/**
 * Called to test whether or not the embedded workbook was correctly
 * updated. This method simply recovers the first cell from the first row
 * of the first workbook and tests the value it contains.
 * <p/>/*from   ww w.j  av  a  2  s  .com*/
 * Note that execution will not continue up to the assertion as the
 * embedded workbook is now corrupted and causes an IllegalArgumentException
 * with the following message
 * <p/>
 * <em>java.lang.IllegalArgumentException: Your InputStream was neither an
 * OLE2 stream, nor an OOXML stream</em>
 * <p/>
 * to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
 * is executed.
 *
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 *                             Rather
 *                             than use the specific classes (HSSF/XSSF) to handle the embedded
 *                             workbook this method uses those defeined in the SS stream. As
 *                             a result, it might be the case that a SpreadsheetML file is
 *                             opened for processing, throwing this exception if that file is
 *                             invalid.
 * @throws java.io.IOException Thrown if a problem occurs in the underlying
 *                             file system.
 */
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
        pIter = embeddedDocs.iterator();
        while (pIter.hasNext()) {
            pPart = pIter.next();
            if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
                    || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {
                workbook = WorkbookFactory.create(pPart.getInputStream());
                sheet = workbook.getSheetAt(SHEET_NUM);
                row = sheet.getRow(ROW_NUM);
                cell = row.getCell(CELL_NUM);
                assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
            }
        }
    }
}