All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.welemski.wrench.poi.util.SpreadsheetCell Maven / Gradle / Ivy

/*
 * The MIT License
 *
 * Copyright 2016 welemski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.welemski.wrench.poi.util;

import java.text.DecimalFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;

/**
 *
 * @author Lemuel Raganas
 */
public class SpreadsheetCell {
    private Cell cell;
    private boolean evaluate;
    
    private boolean numericPrecision = true;
    
    public SpreadsheetCell(Cell cell){
        this.cell = cell;
    }
    
    public Cell getCell(){
        return this.cell;
    }
    
    public String getStringValue(){
        
        if(this.cell == null){
            return "";
        }
        
        switch(this.cell.getCellType()){
            case Cell.CELL_TYPE_BLANK :
                return "";
            case Cell.CELL_TYPE_BOOLEAN :
                Boolean b = cell.getBooleanCellValue();
                return b.toString();
            case Cell.CELL_TYPE_ERROR :
                return "";
            case Cell.CELL_TYPE_FORMULA :
                if(this.evaluate){
                    return cell.getRichStringCellValue().getString();
                }
                return cell.getCellFormula();
            case Cell.CELL_TYPE_NUMERIC :
                
                if(DateUtil.isCellDateFormatted(cell)){
                    return cell.getDateCellValue().toString();
                }
                
                Double doubleValue = cell.getNumericCellValue();
                if(!this.numericPrecision){
                    double numval = cell.getNumericCellValue();
                    DecimalFormat dcFormat = new DecimalFormat("###");
                    String dcs = dcFormat.format(numval);
                    return dcs;
                }
                
                return doubleValue.toString();
            default:
                return cell.getStringCellValue();
        }
    }
    
    public double getNumericValue(){
        
        if(this.cell == null){
            return 0;
        }
        
        switch(this.cell.getCellType()){
            case Cell.CELL_TYPE_BLANK :
                return 0;
            case Cell.CELL_TYPE_BOOLEAN :
                if(cell.getBooleanCellValue()){
                    return 1;
                } else {
                    return 0;
                }
            case Cell.CELL_TYPE_ERROR :
                return 0;
            case Cell.CELL_TYPE_FORMULA :
                if(evaluate){
                    FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
                    return evaluator.evaluate(cell).getNumberValue();
                }
                return cell.getNumericCellValue();
            case Cell.CELL_TYPE_STRING :
                try{
                    Double.parseDouble(cell.getStringCellValue());
                } catch (NumberFormatException ex){
                    return 0;
                }
            case Cell.CELL_TYPE_NUMERIC:
                
                if(DateUtil.isCellDateFormatted(cell)){
                    return ((Long)cell.getDateCellValue().getTime()).doubleValue();
                }
                
                if(!this.numericPrecision){
                    int dint = (int) cell.getNumericCellValue();
                    return dint;
                }
                return cell.getNumericCellValue();
                
            default:
                return 0.0;
        }
    }
    
    public Date getDateValue(){
        if(this.cell == null){
            return null;
        }
        
        if(this.cell.getCellType() == Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)){
            return cell.getDateCellValue();
        }
        
        return null;
    }
    
    /**
     * If set to false, will try disable decimal place precision.
     * @param precision 
     */
    public SpreadsheetCell setNumericPrecision(boolean precision){
        this.numericPrecision = precision;
        return this;
    }
    
    public SpreadsheetCell setEvaluate(boolean evaluate){
        this.evaluate = evaluate;
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy