Example usage for org.apache.poi.ss.usermodel DataFormatter DataFormatter

List of usage examples for org.apache.poi.ss.usermodel DataFormatter DataFormatter

Introduction

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

Prototype

public DataFormatter(Locale locale) 

Source Link

Document

Creates a formatter using the given locale.

Usage

From source file:fr.amapj.service.engine.excelreader.ExcelReader.java

License:Open Source License

public List<String[]> readFile(String fileName, int nbCol) throws IOException {
    List<String[]> res = new ArrayList<>();

    FileInputStream file = new FileInputStream(new File(fileName));

    HSSFWorkbook workbook = new HSSFWorkbook(file);

    // Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);

    // Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();

    DataFormatter df = new DataFormatter(Locale.FRANCE);

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();//  ww  w  .  j a  va  2s  .  co m
        String[] strCell = new String[nbCol];

        for (int i = 0; i < strCell.length; i++) {
            strCell[i] = getValue(row, i, df);
        }

        res.add(strCell);

    }

    file.close();

    return res;
}

From source file:fr.amapj.view.views.importdonnees.tools.AbstractImporter.java

License:Open Source License

private void processFile() throws IOException {

    // Get the workbook instance for XLS file
    HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray()));
    DataFormatter df = new DataFormatter(Locale.FRANCE);

    // Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);

    int numCol = getNumCol();

    List<T> existing = getAllDataInDatabase();

    List<T> utilisateurs = new ArrayList<>();

    int lastRowNum = sheet.getLastRowNum();

    for (int numLigne = 2; numLigne <= lastRowNum + 1; numLigne++) {
        Row row = sheet.getRow(numLigne - 1);

        String[] strs = new String[numCol];
        boolean isEmptyLine = true;
        for (int i = 0; i < strs.length; i++) {
            strs[i] = getCell(row, i, df);
            if ((strs[i] != null) && (strs[i].length() > 0)) {
                isEmptyLine = false;/*  w w  w .  ja  v  a  2s  .  com*/
            }
        }

        if (isEmptyLine == false) {

            // On cre le DTO
            T dto = createDto(strs);

            // On vrifie tout d'abord si les elements de base sont bien prsents
            performBasicCheck(dto, numLigne);
            if (errorMessage.size() != 0) {
                return;
            }

            // On verifie ensuite si l'lment est bien compatible avec les autres lignes du fichier 
            checkLineInSameFile(utilisateurs, dto, numLigne);
            if (errorMessage.size() != 0) {
                return;
            }

            // On verifie ensuite si l'lment est bien compatible avec les autres enregistrement de la base         
            checkLineInDataBase(existing, dto, numLigne);
            if (errorMessage.size() != 0) {
                return;
            }

            utilisateurs.add(dto);
        }

    }

    saveInDataBase(utilisateurs);
}

From source file:fsart.diffTools.converter.ToCSV.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 by
 *        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 by the file system.
 *///from  w  ww.  ja  va2  s .com
private void openWorkbook(File file) throws FileNotFoundException, IOException {
    FileInputStream fis = null;
    try {
        System.out.println("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 by cells and create a
        // formatted String encapsulating the cells contents.
        this.workbook = new HSSFWorkbook(fis);
        this.evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter(true);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:fsart.diffTools.converter.ToCSV.java

License:Apache License

/**
 * Open an Excel workbook ready for conversion.
 *
 * @param file An instance of the InputStream class that encapsulates a handle
 *        to a valid Excel workbook. Note that the workbook can be by
 *        either binary (.xls) or SpreadsheetML (.xlsx) format.
 * @throws java.io.IOException Thrown if a problem occurs by the file system.
 *//*from   ww  w  . j  a v  a 2 s  .  c  om*/
public void openWorkbook(InputStream fis) throws IOException {
    try {
        System.out.println("Opening workbook in input stream");

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

From source file:fsart.diffTools.converter.ToCSV.java

License:Apache License

/**
 * Open an Excel workbook ready for conversion.
 *
 * @param file An instance of the URL class that point on a file 
 *        to a valid Excel workbook. Note that the workbook can be by
 *        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 by the file system.
 */// www  . j av a  2s. com
public void openWorkbook(URI file) throws Exception {
    FileInputStream fis = null;
    try {
        File fic = new File(file);
        fis = new FileInputStream(fic);

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

From source file:gov.nih.nci.evs.app.neopl.ExcelToCSV.java

License:Open Source License

private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;/*from ww w. j a v  a  2s.c om*/
    try {
        fis = new FileInputStream(file);
        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:it.greenvulcano.excel.reader.BaseReader.java

License:Open Source License

public void processExcel(InputStream in) throws ExcelException, InterruptedException {
    cleanUp();/*from  www  .java  2  s.  c o m*/
    Workbook workbook = null;

    try {
        // Open the workbook and then create the FormulaEvaluator and
        // DataFormatter instances that will be needed to, respectively,
        // force evaluation of formula found in cells and create a
        // formatted String encapsulating the cells contents.
        workbook = WorkbookFactory.create(in);
        evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        formatter = new DataFormatter(true);

        processExcel(workbook);
    } catch (ExcelException exc) {
        throw exc;
    } catch (Exception exc) {
        ThreadUtils.checkInterrupted(exc);
        throw new ExcelException("Error parsing WorkBook", exc);
    } finally {
        workbook = null;
        formatter = null;
        evaluator = null;
    }
}

From source file:no.hild1.bank.KonverterMottagerregister.java

License:Apache License

private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;/*  w ww  .  j ava2 s . c  om*/
    try {
        System.out.println("pner arbeidsbok [" + file.getName() + "]");
        fis = new FileInputStream(file);
        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:no.hild1.excelsplit.ES.java

private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;/*from w  w  w. jav a2 s .co m*/
    try {
        System.out.println("pner arbeidsbok [" + file.getName() + "]");
        fis = new FileInputStream(file);
        this.inputWorkbook = WorkbookFactory.create(fis);
        this.evaluator = this.inputWorkbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter(true);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:org.abhishek.simplicitas.util.common.ExcelUtils.java

License:Apache License

/**
 * Open an Excel workbook ready for conversion.
 * <p>/* www  .ja v  a 2  s.c  o m*/
 * Converts all exceptions to Runtime alternatives thereby removing the need
 * for explicit checks.
 * <p>
 * Handles {@link FileNotFoundException} thrown if the file cannot be
 * located.
 * <p>
 * Handles {@link IOException} thrown if a problem occurs in the file
 * system.
 * <p>
 * Handles {@link InvalidFormatException} thrown if invalid xml is found
 * whilst parsing an input SpreadsheetML file.
 *
 * @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.
 */
private void openWorkbook(File file) {
    FileInputStream fis = null;
    try {
        System.out.println("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);
    } catch (InvalidFormatException ex) {
        handleCheckedException(ex);
    } catch (IOException ex) {
        handleIOException(ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                handleIOException(ex);
            }
        }
    }
}