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:org.jreserve.gui.excel.template.dataimport.createwizard.ExcelNameReader.java

License:Open Source License

@Override
public void run() {
    synchronized (lock) {
        try {/*from  w w w  . j  a  v  a2  s.  c  o  m*/
            names = new ArrayList<String>();
            Workbook wb = WorkbookFactory.create(file);
            for (Name name : ExcelUtil.getReferenceNames(wb))
                names.add(name.getNameName());
        } catch (Exception ex2) {
            this.ex = ex2;
        }
    }
}

From source file:org.jreserve.gui.poi.ExcelReader.java

License:Open Source License

@Override
public void run() {
    synchronized (lock) {
        try {//www. jav a  2 s .c o m
            wb = WorkbookFactory.create(file);
        } catch (Exception ex2) {
            this.ex = ex2;
        }
    }
}

From source file:org.matonto.etl.rest.impl.DelimitedRestImpl.java

License:Open Source License

/**
 * Converts the specified number of rows of a Excel file into JSON and returns
 * them as a String./* w  w w  . j  a va  2 s  .  c  o  m*/
 *
 * @param input the Excel file to convert into JSON
 * @param numRows the number of rows from the Excel file to convert
 * @return a string with the JSON of the Excel rows
 * @throws IOException excel file could not be read
 * @throws InvalidFormatException file is not in a valid excel format
 */
private String convertExcelRows(File input, int numRows) throws IOException, InvalidFormatException {
    Workbook wb = WorkbookFactory.create(input);
    // Only support single sheet files for now
    Sheet sheet = wb.getSheetAt(0);
    DataFormatter df = new DataFormatter();
    JSONArray rowList = new JSONArray();
    String[] columns;
    for (Row row : sheet) {
        if (row.getRowNum() <= numRows) {
            columns = new String[row.getPhysicalNumberOfCells()];
            int index = 0;
            for (Cell cell : row) {
                columns[index] = df.formatCellValue(cell);
                index++;
            }
            rowList.add(columns);
        }
    }

    return rowList.toString();
}

From source file:org.matonto.etl.rest.impl.DelimitedRestImplTest.java

License:Open Source License

private List<String> getExcelResourceLines(String fileName) {
    List<String> expectedLines = new ArrayList<>();
    try {/*from  ww  w. j a v  a 2 s.c om*/
        Workbook wb = WorkbookFactory.create(getClass().getResourceAsStream("/" + fileName));
        Sheet sheet = wb.getSheetAt(0);
        DataFormatter df = new DataFormatter();
        int index = 0;
        for (Row row : sheet) {
            String rowStr = "";
            for (Cell cell : row) {
                rowStr += df.formatCellValue(cell);
            }
            expectedLines.add(index, rowStr);
            index++;
        }
    } catch (IOException | InvalidFormatException e) {
        e.printStackTrace();
    }
    return expectedLines;
}

From source file:org.matonto.etl.service.delimited.DelimitedConverterImpl.java

License:Open Source License

@Override
public Model convert(ExcelConfig config) throws IOException, MatOntoException {
    String[] nextRow;/*from w  ww  .j  av  a 2 s  .  co m*/
    Model convertedRDF = modelFactory.createModel();
    ArrayList<ClassMapping> classMappings = parseClassMappings(config.getMapping());

    try {
        Workbook wb = WorkbookFactory.create(config.getData());
        Sheet sheet = wb.getSheetAt(0);
        DataFormatter df = new DataFormatter();
        boolean containsHeaders = config.getContainsHeaders();
        long offset = config.getOffset();
        Optional<Long> limit = config.getLimit();

        //Traverse each row and convert column into RDF
        for (Row row : sheet) {
            // If headers exist or the row is before the offset point, skip the row
            if ((containsHeaders && row.getRowNum() == 0)
                    || row.getRowNum() - (containsHeaders ? 1 : 0) < offset
                    || (limit.isPresent() && row.getRowNum() >= limit.get() + offset)) {
                continue;
            }
            nextRow = new String[row.getPhysicalNumberOfCells()];
            int cellIndex = 0;
            for (Cell cell : row) {
                nextRow[cellIndex] = df.formatCellValue(cell);
                cellIndex++;
            }
            writeClassMappingsToModel(convertedRDF, nextRow, classMappings);
        }
    } catch (InvalidFormatException e) {
        throw new MatOntoException(e);
    }

    return convertedRDF;
}

From source file:org.meveo.admin.parse.xls.XLSFile.java

License:Open Source License

public void parse() throws IOException {
    Workbook w;/*from w w  w.  j a v a2s. c  om*/
    try {
        w = WorkbookFactory.create(new FileInputStream(file));
        // Get the first sheet
        Sheet sheet = w.getSheetAt(0);
        // Loop over first 10 column and lines

        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            String[] strs = new String[row.getPhysicalNumberOfCells()];

            int cellCtr = 0;
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                strs[cellCtr++] = cell.getStringCellValue();
            }

            contexts.add(strs);
        }
    } catch (InvalidFormatException e) {
        log.error("invalid file format ", e);
    }
}

From source file:org.meveo.commons.utils.ExcelToCsv.java

License:Apache License

/**
 * Open an Excel workbook ready for conversion.
 *
 * @param file An instance of the File class that encapsulates a handle
 *        to a valid Excel workbook. Note that the workbook can be in
 *        either binary (.xls) or SpreadsheetML (.xlsx) format.
 * @throws java.io.FileNotFoundException Thrown if the file cannot be located.
 * @throws java.io.IOException Thrown if a problem occurs in the file system.
 * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException Thrown
 *         if invalid xml is found whilst parsing an input SpreadsheetML
 *         file.//ww w.j a  v  a2  s  .co m
 */
private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;
    try {
        log.debug("Opening workbook [" + file.getName() + "]");

        fis = new FileInputStream(file);

        // Open the workbook and then create the FormulaEvaluator and
        // DataFormatter instances that will be needed to, respectively,
        // force evaluation of forumlae found in cells and create a
        // formatted String encapsulating the cells contents.
        this.workbook = WorkbookFactory.create(fis);
        this.evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter(true);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:org.mifos.dmt.excel.cleanup.PurgeEmptyRowsTest.java

License:Open Source License

@Test
public void processEmptyRowsTrue() throws InvalidFormatException, IOException {
    status = true;//from w w  w.ja v a  2 s .c om
    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplatePassTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    purgeEmptyRows = new PurgeEmptyRows(workbook);
    try {
        workbook = purgeEmptyRows.processEmptyRows();
    } catch (DMTException e) {
        status = false;
    } finally {

        Assert.assertTrue(status);
    }
}

From source file:org.mifos.dmt.excel.cleanup.PurgeEmptyRowsTest.java

License:Open Source License

@Test
public void processEmptyRowsFalse() throws InvalidFormatException, IOException {
    status = true;/*from  w  w w  .j  a va  2  s.c  o  m*/
    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplateFailTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    purgeEmptyRows = new PurgeEmptyRows(workbook);
    try {
        workbook = purgeEmptyRows.processEmptyRows();
    } catch (DMTException e) {
        status = false;
    } finally {

        Assert.assertFalse(status);
    }
}

From source file:org.mifos.dmt.excel.columnValidator.ColumnStructure.java

License:Open Source License

public ColumnStructure(Workbook workbook, String baseTemplate) throws InvalidFormatException, IOException {
    this.workbook = workbook;
    InputStream inputstream = new FileInputStream(baseTemplate);
    this.baseworkbook = WorkbookFactory.create(inputstream);
}