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

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

Introduction

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

Prototype

public Format createFormat(Cell cell) 

Source Link

Document

Create and return a Format based on the format string from a cell's style.

Usage

From source file:org.testeditor.core.importer.ExcelFileImporter.java

License:Open Source License

/**
 * gets the testData from a numeric cell.
 * /* w ww  .java  2  s .  c o m*/
 * @param testDataRow
 *            TestDataRow
 * @param cell
 *            HSSFCell
 */
protected void getTestDataNumericCell(TestDataRow testDataRow, HSSFCell cell) {
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
        double value = cell.getNumericCellValue();
        if (HSSFDateUtil.isValidExcelDate(value)) {
            Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
            DateFormat format = new SimpleDateFormat(JAVA_TOSTRING);
            testDataRow.add(format.format(date));
        }
    } else {
        DataFormatter df = new DataFormatter();
        Format cellFormat = df.createFormat(cell);
        if (cellFormat instanceof DecimalFormat) {
            String pattern = ((DecimalFormat) cellFormat).toPattern();
            DecimalFormat dFormatter = new DecimalFormat(pattern);
            testDataRow.add(dFormatter.format(cell.getNumericCellValue()));
        } else {
            testDataRow.add(String.valueOf(cell.getNumericCellValue()));
        }
    }
}