Package org.apache.poi.ss.usermodel

Examples of org.apache.poi.ss.usermodel.DataFormatter


    private void processSheet(Sheet sheet,
                              List< ? extends DataListener> listeners) {
        int maxRows = sheet.getLastRowNum();

        CellRangeAddress[] mergedRanges = getMergedCells(sheet);
        DataFormatter formatter = new DataFormatter();

        for ( int i = 0; i <= maxRows; i++ ) {
            Row row = sheet.getRow( i );
            int lastCellNum = row != null ? row.getLastCellNum() : 0;
            newRow( listeners, i, lastCellNum );

            for ( int cellNum = 0; cellNum < lastCellNum; cellNum++ ) {
                Cell cell = row.getCell(cellNum);
                if (cell == null)
                    continue;
                double num = 0;

                CellRangeAddress merged = getRangeIfMerged( cell,
                        mergedRanges );

                if ( merged != null ) {

                    Cell topLeft = sheet.getRow(merged.getFirstRow()).getCell(merged.getFirstColumn());
                    newCell( listeners,
                            i,
                            cellNum,
                            formatter.formatCellValue(topLeft),
                            topLeft.getColumnIndex() );
                } else {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        num = cell.getNumericCellValue();
                    }
                    if ( Math.abs(num) - Math.ceil(num) != 0 ) {
                        newCell(listeners, i, cellNum, String.valueOf(num), DataListener.NON_MERGED );
                    } else {
                        newCell( listeners,
                                i,
                                cellNum,
                                formatter.formatCellValue(cell),
                                DataListener.NON_MERGED );
                    }
                }
            }
        }
View Full Code Here


    private void processSheet( Sheet sheet,
                               List<? extends DataListener> listeners ) {
        int maxRows = sheet.getLastRowNum();

        CellRangeAddress[] mergedRanges = getMergedCells( sheet );
        DataFormatter formatter = new DataFormatter( Locale.ENGLISH );
        FormulaEvaluator formulaEvaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();

        for ( int i = 0; i <= maxRows; i++ ) {
            Row row = sheet.getRow( i );
            int lastCellNum = row != null ? row.getLastCellNum() : 0;
            newRow( listeners, i, lastCellNum );

            for ( int cellNum = 0; cellNum < lastCellNum; cellNum++ ) {
                Cell cell = row.getCell( cellNum );
                if ( cell == null ) {
                    continue;
                }
                double num = 0;

                CellRangeAddress merged = getRangeIfMerged( cell,
                                                            mergedRanges );

                if ( merged != null ) {
                    Cell topLeft = sheet.getRow( merged.getFirstRow() ).getCell( merged.getFirstColumn() );
                    newCell( listeners,
                             i,
                             cellNum,
                             formatter.formatCellValue( topLeft ),
                             topLeft.getColumnIndex() );

                } else {
                    switch ( cell.getCellType() ) {
                        case Cell.CELL_TYPE_FORMULA:
                            String cellValue = null;
                            try {
                                newCell( listeners,
                                         i,
                                         cellNum,
                                         formatter.formatCellValue( cell,  formulaEvaluator),
                                         DataListener.NON_MERGED );
                            } catch ( RuntimeException e ) {
                                // This is thrown if an external link cannot be resolved, so try the cached value
                                log.warn( "Cannot resolve externally linked value: " + formatter.formatCellValue( cell ) );
                                String cachedValue = tryToReadCachedValue( cell );
                                newCell( listeners,
                                         i,
                                         cellNum,
                                         cachedValue,
                                         DataListener.NON_MERGED );
                            }
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            num = cell.getNumericCellValue();
                        default:
                            if ( num - Math.round( num ) != 0 ) {
                                newCell( listeners,
                                         i,
                                         cellNum,
                                         String.valueOf( num ),
                                         DataListener.NON_MERGED );
                            } else {
                                newCell( listeners,
                                         i,
                                         cellNum,
                                         formatter.formatCellValue( cell ),
                                         DataListener.NON_MERGED );
                            }
                    }
                }
            }
View Full Code Here

        }
        finishSheet( listeners );
    }

    private String tryToReadCachedValue( Cell cell ) {
        DataFormatter formatter = new DataFormatter( Locale.ENGLISH );
        String cachedValue;
        switch ( cell.getCachedFormulaResultType() ) {
            case Cell.CELL_TYPE_NUMERIC:
                double num = cell.getNumericCellValue();
                if ( num - Math.round( num ) != 0 ) {
                    cachedValue = String.valueOf( num );
                } else {
                    cachedValue = formatter.formatCellValue( cell );
                }
                break;

            case Cell.CELL_TYPE_STRING:
                cachedValue = cell.getStringCellValue();
View Full Code Here

        this.extractor = extractor;
        extractor.setFormulasNotResults(false);
        extractor.setLocale(locale);
       
        if(locale == null) {
           formatter = new DataFormatter();
        } else  {
           formatter = new DataFormatter(locale);
        }
    }
View Full Code Here

    public XSSFExcelExtractorDecorator(
            ParseContext context, XSSFExcelExtractor extractor, Locale locale) {
        super(context, extractor, TYPE);

        this.extractor = extractor;
        formatter = new DataFormatter(locale);
    }
View Full Code Here

        this.extractor = extractor;
        extractor.setFormulasNotResults(false);
        extractor.setLocale(locale);
       
        if(locale == null) {
           formatter = new DataFormatter();
        } else  {
           formatter = new DataFormatter(locale);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.usermodel.DataFormatter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.